混合jpa继承策略 - inheritanceType.JOINED with inheritanceType.SINGLE_TABLE

时间:2012-04-03 23:58:10

标签: java hibernate jpa

我的类结构看起来像这样......我在这里实现了两个不同的策略,但是根类的继承策略,即在整个层次结构中使用了InheritanceType.JOINED ......

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "typeName", discriminatorType = DiscriminatorType.STRING, length =    100)
@Table(name="table_A")
public abstract class A{
...
}


@Entity
@Table(name = "table_B")
@PrimaryKeyJoinColumn(name = "ID_B")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorValue("SVC")
public abstract class B extends A {
...
}

@Entity
@DiscriminatorValue("C")
public class C extends B {
... 
}

@Entity
@DiscriminatorValue("D")
public class D extends B {
...
}

当我创建一个'D'实例并试图坚持它时,hibernate正在寻找一个名为'D'的表...... 我发现有几个人问同样的问题......但答案对我没有帮助...

mixing joined and single table inheritance and querying for all objects - 同样的问题..
How to mix inheritance strategies with JPA annotations and Hibernate? - 将single_table与join相混合..这对我的情况没有帮助..

1 个答案:

答案 0 :(得分:2)

JPA规范不允许您在继承树中混合策略;它要求您在根实体中设置继承策略。 JDO是唯一允许混合策略的规范。您可以找到允许它的JPA实现,但它是不可移植的