通过注释的多态JPA最佳实践 [英] Best Practices for Polymorphic JPA via Annotations

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

问题描述

我正在尝试使用带有JPA批注的Hibernate来设置多态行为.

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?

推荐答案

我不确定您的问题是否有绝对答案,这实际上取决于您的特定情况以及从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的参数或 Query操作.映射的 超类不能成为目标 持久的关系.

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注释(或 mapped-superclass 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.

我认为您还应该阅读各种继承策略,以表示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继承映射策略
    • JPA 1.0 specification
      • 2.1.9 Inheritance
      • 2.1.10 Inheritance Mapping Strategies
      • 2.2.4. Mapping inheritance
      • 2.2.4.4. Inherit properties from superclasses

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

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