【发布时间】:2011-04-24 07:44:00
【问题描述】:
根据Hibernate 参考文档,在使用 Hibernate 的 XML 元数据时应该可以混合不同的继承映射策略:
http://docs.jboss.org/hibernate/stable/core/reference/en/html/inheritance.html#inheritance-mixing-tableperclass-tablepersubclass
但是,Hibernate Annotations Reference Guide 的相应部分并未涵盖:http://docs.jboss.org/hibernate/stable/annotations/reference/en/html/entity.html#d0e1168
另一方面,JavaDocs 建议混合继承策略应该是可能的。例如在 javax.persistence.DiscriminatorColumn 它说:
策略和鉴别器列仅在实体类层次结构的根或应用不同继承策略的子层次结构中指定。
以下是我尝试实现的映射的示例。我想在层次结构的根附近使用 table-per-sub-class 映射,但在叶子附近更改为 table-per-class-hierarchy 映射。下面是一些示例代码:
@Entity
@Inheritance( strategy = InheritanceType.JOINED )
public abstract class A implements Serializable
{
@Id
private String id;
// other mapped properties...
}
@Entity
@Inheritance( strategy = InheritanceType.SINGLE_TABLE )
public class BB extends A
{
// other mapped properties and associations...
}
@Entity
public class BB1 extends BB
{
// other stuff, not necessarily mapped...
}
@Entity
public class BB2 extends BB
{
// other stuff, not necessarily mapped...
}
@Entity
@Inheritance( strategy = InheritanceType.SINGLE_TABLE )
public class CC extends A
{
// other mapped properties and associations...
}
@Entity
public class CC1 extends CC
{
// other stuff, not necessarily mapped...
}
...
我对这个映射的期望是正好有 3 个表:A、BB 和 CC。 BB 和 CC 都应该有一个名为 DTYPE 的默认鉴别器列。它们还应提供所有映射属性所需的所有列以及它们各自子类的关联。
相反,类层次结构似乎始终使用 table-per-subclass 继承策略。 IE。我为上面提到的每个实体都有一个自己的表。我想避免这种情况,因为类层次结构的叶子是非常轻量级的,并且为它们每个单独的表似乎有点过头了!
我是否忽略了什么?任何建议都非常感谢!我很乐意提供更多信息...
【问题讨论】:
-
这在 Hibernate 3.6.3 上对我不起作用
-
@Meeque 如果 A 是“TABLE_PER_CLASS”并且您希望 C 类(以及它下面的层次结构)相同,并且希望 SINGLE_TABLE 仅用于植根于 BB 类的层次结构,该怎么办?在您下面的解决方案中,您已经反转了解决方案的问题,您认为我提到的变化可以解决吗
标签: hibernate inheritance jpa annotations hierarchy