通过注释最佳Practises多态JPA [英] Best Practises for Polymorphic JPA via Annotations

查看:468
本文介绍了通过注释最佳Practises多态JPA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用带有JPA注解休眠设置多态行为。

I'm trying to set up polymorphic behaviour using Hibernate with JPA annotations.

这似乎是合理的(甚至必要的)来创建一个(抽象)类封装的继承层次参与的持久性所需要的状态和行为;例如:

It seems sensible (maybe even necessary) to create an (abstract) class encapsulating the state and behaviours required for the inheritance hierarchy to participate in persistence; for example


  • 我需要注释的Id属性,我可以在一个接口未做现场市民不要做,为了避免在超异常'对实体未指定标识符

  • 我需要的子类,如果我使用表每类层次结构的方法来提供DiscriminatorValue

从连接到持久这种方式是好还是坏实践中抽象类继承?哪些优点/缺点?是否有最佳做法,围绕继承处理JPA的?

Is inheriting from an abstract class coupled to persistence in this way a good or bad practise? What are the pros/cons? Are there best practises around handling inheritance in JPA?

推荐答案

我不知道有一个绝对的答案,你的问题,这真的取决于你的具体情况和你想从一个重新present什么观点OO点。提供了一些例子可能会有所帮助。

I'm not sure there is an absolute answer to your questions, it really depends on your particular situation and what you want to represent from a OO point of view. Providing some example would probably help.

但我怀疑,你可能想了解在映射超。下面是从JPA规范一点摘录:

But I suspect that you might want to read about the Mapped Superclasses. Here is a little extract from the JPA specification:

这是实体可以从继承
  超提供持久
  实体状态和映射信息,
  但是这不是本身的实体。
  通常,这样的目的
  映射超类是定义状态
  这是常见的,映射信息
  多个实体类。

An entity may inherit from a superclass that provides persistent entity state and mapping information, but which is not itself an entity. Typically, the purpose of such a mapped superclass is to define state and mapping information that is common to multiple entity classes.

映射超,不像一个实体,
  不可查询并且不能传递
  作为参数传递给的EntityManager
  查询操作。映射的
  超类不能是一个目标
  持久关系。

A mapped superclass, unlike an entity, is not queryable and cannot be passed as an argument to EntityManager or Query operations. A mapped superclass cannot be the target of a persistent relationship.

既抽象又具体类可能
  被指定为映射超类。
  在 MappedSuperclass 注释(或
  映射-超 XML描述符
  元件)用于指定映射
  超类。

Both abstract and concrete classes may be specified as mapped superclasses. The MappedSuperclass annotation (or mapped-superclass XML descriptor element) is used to designate a mapped superclass.

一个类指定为
   MappedSuperclass 没有单独的
  表为它定义。它的映射
  信息被施加到各实体
  从它继承。

A class designated as MappedSuperclass has no separate table defined for it. Its mapping information is applied to the entities that inherit from it.

而且我觉得你也应该了解不同的继承策略在数据库级别重新present继承因为他们不支持JPA(每类层次结构单一的表,每个具体类表,加入了子类)提供对多态查询相同的支持级别。 JPA规范涵盖了这个体面。

And I think that you should also read about the various inheritance strategies to represent inheritance at the database level (single table per class hierarchy, table per concrete class, joined subclass) supported by JPA as they don't provide the same level of support for polymorphic queries. The JPA specification covers this decently.

下面的相关引用。


  • JPA 1.0规范

    • 2.1.9继承

    • 2.1.10的继承映射策略

    • 2.2.4. Mapping inheritance
    • 2.2.4.4. Inherit properties from superclasses

    这篇关于通过注释最佳Practises多态JPA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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