我有一个层次化的 JPA 映射,有几个类深。像这样的事情
@Entity
@Inheritance(strategy= InheritanceType.SINGLE_TABLE)
public abstract class BaseEntityClass implements Serializable {
// lots of stuff common to all my entities.
}
@Entity
@Inheritance(strategy= InheritanceType.TABLE_PER_CLASS)
public abstract class EntityTypeOne extends BaseEntityClass {
// stuff common to EntityTypeOne
}
@Entity
@Inheritance(strategy= InheritanceType.TABLE_PER_CLASS)
public abstract class EntityTypeTwo extends BaseEntityClass {
// stuff common to EntityTypeTwo
}
我知道您使用 @Inheritence 方法和父类(super class)来定义映射策略。但这如何适用于深层层次结构呢?我希望所有叶类都映射到它们自己的表。我应该使用 BaseEntityClass 执行 SINGLE_TABLE 吗?
最佳答案
您的基类应标记为 TABLE_PER_CLASS
。这是 OpenJPA 中的示例:
@Entity
@Table(name="MAG")
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class Magazine {
...
}
@Entity
@Table(name="TABLOID")
public class Tabloid
extends Magazine {
...
}
根据JPA JavaDocs ,@Inheritance
注释应该在类层次结构的根上指定:
Defines the inheritance strategy to be used for an entity class hierarchy. It is specified on the entity class that is the root of the entity class hierarchy.
规范中没有指定此注释是否可以放置在子类上,因此其行为未定义。
因此,如果您有以下层次结构,请回答您在评论中提出的问题:
SubSub -> Sub -> Base
只需注释Base
与 @Inheritance(strategy = TABLE_PER_CLASS)
.
关于java - JPA深度继承注解属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8825577/