Java / Hibernate JPA:InheritanceType.TABLE_PER_CLASS和ID [英] Java/Hibernate JPA: InheritanceType.TABLE_PER_CLASS and IDs
问题描述
假设我有这些类:
AbstractPerson
| - > ConcreteEmployee
| - > ConcreteCustomer
有什么方法可以让具体类拥有独立的ID吗?
我使用了InheritanceType.TABLE_PER_CLASS。
从Hibernate Annotations Reference Guide: p>
2.2.4.1。 Table per class
这个策略有许多缺点(特别是带有多态查询和
关联的
)在JPA $ b中解释$ b规范,Hibernate引用
文档,Hibernate in Action,
和许多其他地方。 Hibernate使用
工作,其中大多数人使用SQL UNION
查询实现这个
策略。它通常用于
的顶层继承层次:
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Flight implements Serializable {...}
此策略支持一对多
关联,只要它们是
双向。 此策略不支持IDENTITY
生成器
策略:该ID必须在多个表中共享
。因此,当使用这种策略时,
应该是
不使用AUTO
也不是IDENTITY
所以我怕你想要的东西不被支持(并且我建议使用GenerationType .TABLE
)。I'm using Hibernate JPA.
Suppose I have these classes:
AbstractPerson |--> ConcreteEmployee |--> ConcreteCustomer
Is there any way to make the concrete classes have independent IDs?
I'm using InheritanceType.TABLE_PER_CLASS.
解决方案From the Hibernate Annotations Reference Guide:
2.2.4.1. Table per class
This strategy has many drawbacks (esp. with polymorphic queries and associations) explained in the JPA spec, the Hibernate reference documentation, Hibernate in Action, and many other places. Hibernate work around most of them implementing this strategy using
SQL UNION
queries. It is commonly used for the top level of an inheritance hierarchy:@Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) public class Flight implements Serializable { ... }
This strategy supports one-to-many associations provided that they are bidirectional. This strategy does not support the
IDENTITY
generator strategy: the id has to be shared across several tables. Consequently, when using this strategy, you should not useAUTO
norIDENTITY
.So I'm afraid what you want is not supported (and I suggest to use
GenerationType.TABLE
).这篇关于Java / Hibernate JPA:InheritanceType.TABLE_PER_CLASS和ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!