JPA / Hibernate差异MappedSuperclass和实体抽象类 [英] JPA/Hibernate Difference MappedSuperclass and Entity Abstract Class

查看:111
本文介绍了JPA / Hibernate差异MappedSuperclass和实体抽象类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道 MappedSuperclass 实体抽象类之间的区别是什么时候想从一个在 Hibernate 中的超类。我知道 Hibernate 不会在数据库中为 MappedSuperclass 创建表。我读了 JavaEE doc 抽象实体像具体实体,但不能实例化。由于它们无法实例化,因此我推断数据库中存在没有匹配的表。如果是这样,使用 MappedSuperclass 和一个<$ c $之间的区别实际上是什么? c>实体抽象类以及每种情况下对软件和数据库有什么影响?

解决方案

MappedSuperclass使用继承来实现字段和代码重用。例如,如果你想要所有的实体都有一个 Long id 和一个 Long version 字段,你可以让它们所有这些扩展了包含这两个字段的 BaseEntity 类,它们都带有 MappedSuperclass ,以及它们的getter,setter等。但是,将永远不会有一个与BaseEntity有关联的实体:该关联总是与BaseEntity的特定子类相关联。



父实体用于实体多态性。例如,你可以想象有两种 Message :一个 EmailMessage 和一个 SmsMessage 。两者都会包含来源,目标和正文。但是EmailMessage会有一个电子邮件地址和一个主题,而SmsMessage会有一个电话号码。



你可以想象有一个 Person 包含发送消息集合的实体,类型为 Message 。该集合实际上包含EmailMessage和SmsMessage的实例。 Hibernate会根据用于继承映射的继承策略来决定实例化哪一个:


  • 所有消息都可以存储在同一个表中,并且Hibernate会使用包含消息类型的鉴别器列
  • EmailMessage可以存储在一个表中,而SmsMessage存储在另一个表中
  • 或两个实体(源,目标,主体)共有的字段可以存储在comon表中,第二个表中特定于EmailMessage的字段以及第三个表中特定于SmsMessage的字段。 b $ b

I would like to know what is the difference between MappedSuperclass and Entity Abstract Class when one wants to derive from a super class in Hibernate. I know that Hibernate does not create a Table in the Database for a MappedSuperclass. I read in the JavaEE doc "Abstract entities are like concrete entities but cannot be instantiated". Since they cannot be instantiated, I deduce that there is no matching Table in the Database. Am I right?

If so, what are virtually the difference between using a MappedSuperclass and an Entity Abstract Class and what is the impact on the Software and in the Database in each case?

解决方案

A MappedSuperclass uses inheritance for field and code reuse. For example, if you want all your entities to have a Long id and a Long version field, you could make them all extend a BaseEntity class annotated with MappedSuperclass containing these two fields, along with their getters, setters, etc. But you would never have an entity having an association with a BaseEntity: the association would always be with a specific subclass of BaseEntity.

A parent entity is used for "entity polymorphism". For example, you could imagine having two kinds of Message: an EmailMessage and a SmsMessage. Both would contain a source, a target, and a body. But EmailMessage would have an email address and a subject, whereas SmsMessage would have a phone number.

And you could imagine having a Person entity containing a collection of sent messages, of type Message. The collection would in fact contain instances of EmailMessage and of SmsMessage. Hibernate would decide which one to instantiate depending on the inheritance strategy used for the inheritance mapping:

  • all the messages could be stored in the same table, and Hibernate would use a discriminator column containing the type of the message
  • the EmailMessage could be stored in one table, and the SmsMessage stored in another one
  • or the fields common to both entities (source, target, body) could be stored in a comon table, the fields specific to EmailMessage in a second table, and the fields specific to SmsMessage in a third table.

这篇关于JPA / Hibernate差异MappedSuperclass和实体抽象类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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