java @MappedSuperclass 不是 JPA 中的 @Entity 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36991236/
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-11-03 02:05:03  来源:igfitidea点击:

@MappedSuperclass is not an @Entity in JPA?

javahibernatejpainheritanceorm

提问by Rakim

So I am using DerbyDB and I am setting up some entities. I have a @MappedSuperclasswhich is used as a superclass for some entities (@Entity). More specifically, I have a superclass Userand 3 subclasses namely admin, regularand guest. Now I have a different entity, let's say filethat should reference (as one of its fields) its owner. So I created a field called User owner. The error I get is:

所以我正在使用 DerbyDB 并且我正在设置一些实体。我有一个@MappedSuperclass用作某些实体 ( @Entity)的超类。更具体地说,我有一个超类User和 3 个子类,即admin,regularguest。现在我有一个不同的实体,假设file它应该引用(作为其字段之一)其所有者。所以我创建了一个名为User owner. 我得到的错误是:

Exception Description: [File] uses a non-entity [User] as target entity in the relationship attribute [field owner].

Is there a workaround?

有解决方法吗?

回答by XiCoN JFS

I can suggest two solutions:

我可以建议两种解决方案:

Change Inheritance

改变继承

The exception you get clearly describes your problem: Useris not an entity. Any class declared as superclass with the interface @MappedSuperclasscannot be an entity (in standard JPA - depends on your JPA-provider)... let me point you to an answer I just gave to quite a similar problem

您得到的异常清楚地描述了您的问题:User不是实体。任何声明为具有接口的超类的类@MappedSuperclass都不能是实体(在标准 JPA 中 - 取决于您的 JPA 提供者)……让我指出我刚刚对一个非常相似的问题给出的答案

--> Superclass-Types

-->超类类型

So defining your superclass as an abstract entity will give you the desired behaviour, you described.

因此,您描述的将超类定义为抽象实体将为您提供所需的行为。

Extra:

额外的:

If you choose your inheritance mapping strategy as @Inheritance(strategy = InheritanceType.SINGLE_TABLE)you don't even need multiple database-tables. Here is a good example: JPA Single-Table Inheritance

如果您选择继承映射策略,因为@Inheritance(strategy = InheritanceType.SINGLE_TABLE)您甚至不需要多个数据库表。这是一个很好的例子:JPA 单表继承

Change Model (suggested)

更改模型(建议)

Don't split your user entity in several entities just by their roles. Make an Enumwith all your desired roles and add it as a field to your User-entity. This is widely more common, unless u need your admin, guests etc to be an own object...

不要仅根据角色将您的用户实体拆分为多个实体。做一个Enum与所有您想要的角色,并将其添加为一个字段,你User-entity。这更常见,除非你需要你的管理员、客人等成为自己的对象......

回答by Vlad Mihalcea

As I explained in this article, @MappedSupperclassis different than the @Inheritanceannotation.

正如我在解释这个文章@MappedSupperclass是不是不同的@Inheritance注解。

@MappedSuperclasstells the JPA provider to include the base class persistent properties as if they were declared by the child class extending the superclass annotated with @MappedSuperclass.

@MappedSuperclass告诉 JPA 提供者包含基类持久属性,就好像它们是由扩展用 注释的超类的子类声明的@MappedSuperclass

However, the inheritance is only visible in the OOP world, since, from a database perspective, there's no indication of the base class. Only the child class entity will have an associated mapped table.

然而,继承只在 OOP 世界中可见,因为从数据库的角度来看,没有基类的指示。只有子类实体才会有关联的映射表。

The @Inheritanceannotation is meant to materialize the OOP inheritance model in the database table structure. More, you can query a base class annotated with @Inheritancebut you can't do that for a base class annotated with @MappedSuperclass.

@Inheritance注释是为了兑现在数据库表结构的OOP继承模型。此外,您可以查询带有注释的基类,@Inheritance但不能查询带有注释的基类@MappedSuperclass