答案 0 :(得分:10)
你有什么具体的麻烦?映射类层次结构via joined subclasses非常简单:
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public class A implements Serializable { ... }
@Entity
public class B extends A { ... }
@Entity
@PrimaryKeyJoinColumn(name="A_ID")
public class C extends A { ... }
@Entity
@PrimaryKeyJoinColumn(name="B_ID")
public class D extends B { ... }
更新(根据Michal的评论)。
如果你确实想要使用鉴别器(并且你应该有良好的理由),可以通过将table-per-class-hierarchy strategy与secondary tables混合来实现:
@Entity
@Table(name="A_table")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="entity_type")
@DiscriminatorValue("A")
public class A implements Serializable { ... }
@Entity
@SecondaryTable(name="B_table")
public class B extends A { ... }
@Entity
@SecondaryTable(name="C_table", pkJoinColumns={
@PrimaryKeyJoinColumn(name="A_ID", referencedColumnName="ID")
))
public class C extends A { ... }
@Entity
@SecondaryTable(name="D_table", pkJoinColumns={
@PrimaryKeyJoinColumn(name="B_ID", referencedColumnName="ID")
))
public class D extends B { ... }
此方法的缺点是您必须为映射的每个属性显式指定表:
public class D extends B {
@Column(table="D_table")
private String someProperty;
...
}
答案 1 :(得分:0)
Inheritance between @Entity annotated classes会自动被选中。因此,如果您对A类进行了注释,并且B类也使用@Entity进行了注释并扩展了A,并且C扩展了B并且还注释了@Entity,则所有内容都将被选中。
我没有使用连接子类和鉴别器值,但我确信它与XML中的操作非常类似(使用@DiscriminatorColumn和@DiscriminatorValue)
答案 2 :(得分:0)
这对我来说非常合适:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name="LoanType",discriminatorType="String")
@Table(name = "A")
public class A implements Serializable{
}
@Entity
@Table(name= "B")
@PrimaryKeyJoinColumn(name = "B_ID", referencedColumnName ="A_ID")
public class B extends A{
}
@Entity
@Table(name= "C")
@PrimaryKeyJoinColumn(name = "C_ID", referencedColumnName = "B_ID")
public class C extends B{}