JPA:实现模型层次结构 - @MappedSuperclass与@Inheritance [英] JPA: Implementing Model Hierarchy - @MappedSuperclass vs. @Inheritance

查看:970
本文介绍了JPA:实现模型层次结构 - @MappedSuperclass与@Inheritance的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Play Framework 1.2.4与 PostgreSQL JPA 结合使用。我希望有一个模型层次结构,并看到有这样做的一些替代方案。



我有一个基类(这是抽象的)和两个扩展此类的具体类基类。我不想坚持这个基类,而我想要具体的课程。在基类中,我有另一个Model类作为属性,换句话说,我在我的基类中有 @ManyToOne 关系。

我的问题是实现这个最好的方法是什么?使用 @MappedSuperclass @Inheritance TABLE_PER_CLASS 策略?我有点困惑,因为它们看起来几乎是相同的。



我对于将来可能面临的查询和性能问题也有一些担忧。

解决方案

MappedSuperClass必须用于继承属性,关联和方法。



实体继承必须在有实体和多个子实体时使用。

可以告诉如果你需要一个或另一个回答这个问题:模型中是否有其他实体可以与基类关联?



如果是,那么基类实际上是一个实体,你应该使用实体继承。如果否,那么基类实际上是一个包含几个不相关实体共有的属性和方法的类,并且您应该使用映射超类。

例如,

例如:


  • 您可以有几种消息:短信,电子邮件或电话信息。一个人有一个消息列表。无论消息类型如何,您还可以提供链接到消息的提醒。在这种情况下,消息显然是一个实体,并且必须使用实体继承。

  • 所有的域对象都可以有创建日期,修改日期和ID,因此可以使它们继承从基础AbstractDomainObject类。但是没有实体会与AbstractDomainObject建立关联。它将永远是一个更具体的实体的关联:客户,公司,无论如何。在这种情况下,使用MappedSuperClass是有意义的。


I am using Play Framework 1.2.4 with PostgreSQL and JPA. I would like to have a Model hierarchy and see that there are some alternatives to doing this.

I have a base class (which is abstract) and two concrete classes extending this base class. I don't want to persist this base class while I want to have concrete classes. In the base class, I have another Model classes as properties, in other words, I have @ManyToOne relationships in my base class.

My question is what is the best way of implementing this? Using @MappedSuperclass or @Inheritance with TABLE_PER_CLASS strategy? I am a bit confused as they seem virtually equivalent.

I also have some concerns about querying and performance issues that I might face in future.

解决方案

MappedSuperClass must be used to inherit properties, associations, and methods.

Entity inheritance must be used when you have an entity, and several sub-entities.

You can tell if you need one or the other by answering this questions: is there some other entity in the model which could have an association with the base class?

If yes, then the base class is in fact an entity, and you should use entity inheritance. If no, then the base class is in fact a class that contains attributes and methods that are common to several unrelated entities, and you should use a mapped superclass.

For example:

  • You can have several kinds of messages: SMS messages, email messages, or phone messages. And a person has a list of messages. You can also have a reminder linked to a message, regardless of the kind of message. In this case, Message is clearly an entity, and entity inheritance must be used.
  • All your domain objects could have a creation date, modification date and ID, and you could thus make them inherit from a base AbstractDomainObject class. But no entity will ever have an association to an AbstractDomainObject. It will always be an association to a more specific entity: Customer, Company, whatever. In this case, it makes sense to use a MappedSuperClass.

这篇关于JPA:实现模型层次结构 - @MappedSuperclass与@Inheritance的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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