@MappedSuperclass不是@entity吗? [英] @MappedSuperclass is not an @entity?

查看:900
本文介绍了@MappedSuperclass不是@entity吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 异常描述:[File]使用非实体[User]作为目标关系属性[字段所有者]中的实体。 

有没有解决方法?

解决方案

我可以提出两种解决方案:

更改继承



明确描述你的问题: User 不是一个实体。任何使用接口 @MappedSuperclass 声明为超类的类不能是实体(在标准JPA中 - 取决于您的JPA提供程序) ...我指出你给我一个答案,我刚刚给了一个类似的问题

- > 超类类型



因此,将您的超类定义为抽象实体将会给您想要的行为,您描述过。


如果您选择您的继承映射策略为 @Inheritance(strategy = InheritanceType.SINGLE_TABLE)

另外:



c $ c>你甚至不需要多个数据库表。下面是一个很好的例子: JPA单表继承



更改模型(建议)



不要将您的用户实体拆分为多个实体由他们的角色。用你想要的所有角色创建一个 Enum ,并把它作为一个字段添加到你的 User -entity中。这是非常普遍的,除非你需要你的管理员,客人等成为自己的对象......


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].

Is there a workaround?

解决方案

I can suggest two solutions:

Change Inheritance

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

--> 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

Change Model (suggested)

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不是@entity吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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