java Hibernate 和 JPA 的良好继承策略是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3380190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 01:34:13  来源:igfitidea点击:

What is a good inheritance strategy with Hibernate and JPA?

javahibernateinheritancejpaannotations

提问by blow

I have this situation:

我有这种情况:

I have an entity, Person, that contains all personal details of a person, like birth date, street address, municipality ecc ecc. And I have an entity ClubMemberthat describe a member of a club and contains some field like: registration date, type of member, credit, ecc ecc

我有一个实体,Person,其中包含一个人的所有个人详细信息,例如出生日期、街道地址、市政府 ecc ecc。我有一个ClubMember描述俱乐部成员的实体并包含一些字段,例如:注册日期、成员类型、信用、ecc ecc

So, a ClubMemberis a Person, and I think is correct describe this with a inheritance: ClubMember extends Person, but, what type of strategy?

所以, aClubMember是 a Person,我认为用继承来描述它是正确的: ClubMember extends Person但是,什么类型的策略?

I would obtain two table in database, Personand ClubMember, with ClubMemberthat contain id_personlike an @OneToOnerelationship, but keep the inheritance between entities; is this possible (and is correct)?

我会在数据库中获取两个表,PersonClubMember,其中ClubMember包含id_person一个@OneToOne关系,但保持实体之间的继承;这可能(并且是正确的)吗?

JOINEDis the strategy closest to my target, but I lose the IDfield of table ClubMember, in fact IDbecome the IDof table Person...

JOINED是最接近我目标的策略,但我失去了IDtable的领域ClubMember,实际上ID成为了IDtable Person......

采纳答案by Arthur Ronald

Blow, you should keep performance issueswhen choosing some kind of inheritance strategy. Hereyou can see a good insight about available inheritance strategies. Do not forget a inheritance can be re-writen as a association as follows

Blow,在选择某种继承策略时,您应该保留性能问题在这里您可以看到有关可用继承策略的很好的见解。不要忘记继承可以重写为关联如下

instead of

代替

public class A {}

public class B extends A {}

you can use

您可以使用

public class B {

    private A a;

}

Or you can use MapedSuperClass To map all of your inherited properties whitout using any Hibernate/JPA inheritance strategy. See here

或者,您可以使用 MapedSuperClass 来映射所有继承的属性,而无需使用任何 Hibernate/JPA 继承策略。看这里

Keep in mind when using default JPA/Hibernate annotations, Hibernate always fetch all of subclasses. If you have a single hierarchy, prefer To use Single Table Inheritance strategy. Joined strategy is better designed when you have a complex hierarchybut it suffers due To performance issues when querying for any entity.

请记住,在使用默认的 JPA/Hibernate 注释时,Hibernate 总是获取所有子类。如果您有单个层次结构,则更喜欢使用单表继承策略。当您具有复杂的层次结构时,联接策略设计得更好,但由于查询任何实体时的性能问题,它会受到影响。

回答by Abel

EDIT: below answer assumed NHibernate, apologies beforehand if it doesn't apply to Hibernate

编辑:下面的答案假定为 NHibernate,如果它不适用于 Hibernate,请事先道歉

This is not always trivial, even it can be trivially implemented (see below), and should be considered thoroughly. In my experience, it's best to stick to good old aggregation, or even just fields, where each ClubMember has-a person, instead of is-a person. This may not entirely feelright, but it works easier with your configurations, the CRUD operations and your abstract DAO classes. Auto-mapping tools often don't support subclassing out of the box.

这并不总是微不足道的,即使它可以微不足道地实现(见下文),并且应该彻底考虑。根据我的经验,最好坚持良好的旧聚合,甚至只是字段,每个 ClubMember 都有一个人,而不是一个人。这可能感觉不完全正确,但它在您的配置、CRUD 操作和您的抽象 DAO 类中更容易工作。自动映射工具通常不支持开箱即用的子类化。

Also, people that work with both your database and the DAL, will have an understanding for this mapping (i.e., Person as a one-to-one mapping to ClubMember, add non-null to the Person-property and you have your constraint as well), because it resembles the database more closely. You may argue of course that the whole idea of ORM is to remove this similarity ;).

此外,同时使用您的数据库和 DAL 的人将了解此映射(即,Person 作为到 ClubMember 的一对一映射,将非空添加到Person-property 并且您也有约束),因为它更类似于数据库。当然,您可能会争辩说,ORM 的整个想法就是消除这种相似性;)。

If you want to experiment on this path or if you like to see how it's done and apply it to your situation, Ayende has a nice blog on the subject of NHibernate and inheritance. It's a bit basic, but you'll get the idea. If you use Fluent NHibernate, it becomes a bit easier even, see this brief tutorial.

如果您想在这条道路上进行试验,或者如果您想了解它是如何完成的并将其应用于您的情况,Ayende 有一个关于 NHibernate 和继承主题的不错的博客。这有点基础,但你会明白的。如果您使用 Fluent NHibernate,它甚至会变得更容易一些,请参阅此简短教程

回答by hvgotcodes

I would make ClubMemeber extend Person, as you suggested, and use the table per class heriarchy mapping strategy.

我会让 ClubMemeber 按照您的建议扩展 Person,并使用每个类层次结构映射策略的表。

http://docs.jboss.org/hibernate/stable/core/reference/en/html/inheritance.html#inheritance-tableperclass

http://docs.jboss.org/hibernate/stable/core/reference/en/html/inheritance.html#inheritance-tableperclass