@MappedSuperclass不是JPA中的@Entity吗? [英] @MappedSuperclass is not an @Entity in JPA?
问题描述
因此,我正在使用DerbyDB,并且正在设置一些实体.我有一个@MappedSuperclass
,它用作某些实体(@Entity
)的超类.更具体地说,我有一个超类User
和3个子类,分别是admin
,regular
和guest
.现在,我有一个不同的实体,假设file
应该引用其所有者(作为其字段之一).因此,我创建了一个名为User owner
的字段.
我得到的错误是:
So I am using DerbyDB and I am setting up some entities. I have a @MappedSuperclass
which is used as a superclass for some entities (@Entity
). More specifically, I have a superclass User
and 3 subclasses namely admin
, regular
and guest
. Now I have a different entity, let's say file
that should reference (as one of its fields) its owner. So I created a field called User owner
.
The error I get is:
Exception Description: [File] uses a non-entity [User] as target entity in the relationship attribute [field owner].
有解决方法吗?
推荐答案
我可以提出两种解决方案:
I can suggest two solutions:
您得到的异常清楚地描述了您的问题:User
不是实体.任何用接口@MappedSuperclass
声明为超类的类都不能是实体(在标准JPA中-取决于您的JPA提供者) ...让我指出我刚刚给一个答案的答案.相似的问题
The exception you get clearly describes your problem: User
is not an entity. Any class declared as superclass with the interface @MappedSuperclass
cannot 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
-> 超类类型
因此,您描述了将超类定义为抽象实体将为您提供所需的行为.
So defining your superclass as an abstract entity will give you the desired behaviour, you described.
如果您将继承映射策略选择为@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
,则甚至不需要多个数据库表.这是一个很好的示例: JPA单表继承
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
不要仅根据用户角色将其分成几个实体.制作一个具有所有所需角色的Enum
并将其作为字段添加到您的User
-实体.除非您需要您的管理员,来宾等成为自己的对象,否则这种情况普遍存在.
Don't split your user entity in several entities just by their roles. Make an Enum
with 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...
这篇关于@MappedSuperclass不是JPA中的@Entity吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!