根据 Hibernate 引用文档,在使用 Hibernate 的 XML 元数据时应该可以混合不同的继承映射策略:
http://docs.jboss.org/hibernate/stable/core/reference/en/html/inheritance.html#inheritance-mixing-tableperclass-tablepersubclass
但是,Hibernate 注解引用指南的相应部分并未涵盖以下内容:
http://docs.jboss.org/hibernate/stable/annotations/reference/en/html/entity.html#d0e1168
另一方面,JavaDocs 建议混合继承策略应该是可能的。例如在 javax.persistence.DiscriminatorColumn 中它说:
<小时/>The strategy and the discriminator column are only specified in the root of an entity class hierarchy or subhierarchy in which a different inheritance strategy is applied.
以下是我试图实现的映射的示例。我想在层次结构根部附近使用每个子类一个表映射,但在叶子附近更改为每个类层次结构一个表映射。下面是一些示例代码:
@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
的默认鉴别器列。它们还应该提供其各自子类的所有映射属性和关联所需的所有列。
相反,类层次结构似乎自始至终都使用每个子类一个表继承策略。 IE。我为上面提到的每个实体都有一个自己的表。我想避免这种情况,因为类层次结构的叶子非常轻量,并且为每个叶子都有一个单独的表似乎有点过分了!
<小时/>我是不是忽略了什么?任何建议都将受到高度赞赏!我很乐意提供更多信息...
最佳答案
According to the Hibernate Reference Documentation it should be possible to mix different inheritance mapping strategies when using Hibernate's XML-Metadata (...)
实际上,它并不真正受支持,他们在文档示例中使用辅助表“欺骗”从单表策略切换。引用使用 Hibernate 实现 Java 持久化:
You can map whole inheritance hierarchies by nesting
<union-subclass>
,<sub- class>
, and<joined-subclass>
mapping elements. You can’t mix them — for example, to switch from a table-per-class hierarchy with a discriminator to a normalized table-per-subclass strategy. Once you’ve made a decision for an inheritance strategy, you have to stick to it.This isn’t completely true, however. With some Hibernate tricks, you can switch the mapping strategy for a particular subclass. For example, you can map a class hierarchy to a single table, but for a particular subclass, switch to a separate table with a foreign key mapping strategy, just as with table per subclass. This is possible with the
<join>
mapping element:<hibernate-mapping> <class name="BillingDetails" table="BILLING_DETAILS"> <id>...</id> <discriminator column="BILLING_DETAILS_TYPE" type="string"/> ... <subclass name="CreditCard" discriminator-value="CC"> <join table="CREDIT_CARD"> <key column="CREDIT_CARD_ID"/> <property name="number" column="CC_NUMBER"/> <property name="expMonth" column="CC_EXP_MONTH"/> <property name="expYear" column="CC_EXP_YEAR"/> ... </join> </subclass> <subclass name="BankAccount" discriminator-value="BA"> <property name=account" column="BA_ACCOUNT"/> ... </subclass> ... </class> </hibernate-mapping>
您可以通过注释实现相同的目的:
Java Persistence also supports this mixed inheritance mapping strategy with annotations. Map the superclass
BillingDetails
withInheritanceType.SINGLE_TABLE
, as you did before. Now map the subclass you want to break out of the single table to a secondary table.@Entity @DiscriminatorValue("CC") @SecondaryTable( name = "CREDIT_CARD", pkJoinColumns = @PrimaryKeyJoinColumn(name = "CREDIT_CARD_ID") ) public class CreditCard extends BillingDetails { @Column(table = "CREDIT_CARD", name = "CC_NUMBER", nullable = false) private String number; ... }
我没有测试这个,但你也许可以尝试:
- 使用 SINGLE_TABLE 策略映射 A
- 使用
@SecondaryTable
映射 BB、CC 等注解。
我没有测试过这个,我不知道它是否适用于 BB1、BB2。
引用
- Java 与 Hibernate 的持久性
- 5.1.5 混合继承策略 (p207-p210)
关于hibernate - 如何将继承策略与 JPA 注解和 Hibernate 混合使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3915026/