首页 > 解决方案 > Hibernate继承,两个不同表中的重复ID

问题描述

@Entity
@Table(name = "parent");
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
    public class Parent {

    @Id
    @SequenceGenerator(name = "ME_SEQ", sequenceName = "ME_SEQ", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ME_SEQ")
    @Column(name = "PARENT_ID", columnDefinition = "NUMBER(38,0)", nullable = false, unique = true)
    private Long id;
}

还有一个子实体(单独的表),它有一个指向 Parent ID 的 PK 和 FK

@Entity
@Table(name = "child")
public class Child extends Parent {

    @Id
    @Column(name = "PARENT_ID")
    private Long id;
}

即使有两个单独的表,我也从 Hibernate 收到一个错误:

org.hibernate.mapping.UnionSubclass 不能转换为 org.hibernate.mapping.RootClass

子类中是否不可能有 ID,即使它与父类是不同的表?

标签: javahibernatejpa

解决方案


推荐阅读