Java / Hibernate JPA:InheritanceType.TABLE_PER_CLASS和ID [英] Java/Hibernate JPA: InheritanceType.TABLE_PER_CLASS and IDs

查看:434
本文介绍了Java / Hibernate JPA:InheritanceType.TABLE_PER_CLASS和ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Hibernate JPA。

假设我有这些类:

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

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆