Java/Hibernate JPA: InheritanceType.TABLE_PER_CLASS and IDs

北城余情 提交于 2019-11-27 06:32:39

问题


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.


回答1:


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 use AUTO nor IDENTITY.

So I'm afraid what you want is not supported (and I suggest to use GenerationType.TABLE).



来源:https://stackoverflow.com/questions/3154649/java-hibernate-jpa-inheritancetype-table-per-class-and-ids

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!