我有一个简单的实体的类层次结构,如下所示:

Permissable  (super class)
\
 PermissableResource (sub class)

They are persisted in a table like so:

Permissable
-------------
id          PK
resource    FK Resource(resourceId)

I have another entity called Resource that I want to create an association with. It is persisted in a table like so:

Resource
-------------
resourceId    PK

In my PermissableResource class, I create the association like so:

@JoinColumn(name="resource", referencedColumnName="resourceId")
@ManyToOne
Resource resource;


问题与Resource类有关。我想引用超类Permissable而不是子类PermissableResource。我尝试像这样创建关联:

@OneToOne(mappedBy="resource", cascade= CascadeType.ALL, targetEntity=PermissableResource.class)
private Permissable permissiable;


但这会在部署时导致以下错误:

Exception Description: The attribute [permissiable] in entity class [class com.dv.oa.model.entity.resource.Resource] has a mappedBy value of [resource] which does not exist in its owning entity class [class com.dv.oa.model.entity.permission.permissable.Permissable]. If the owning entity class is a @MappedSuperclass, this is invalid, and your attribute should reference the correct subclass.. Please see server.log for more details.


它在Permissable中寻找关联,而不是在PermissableResource中寻找

我以为,由于我使用了targetEntity@OneToOne属性并将其指向PermissableResource.class,因此可以使用。如何在我的实体中保留超类引用,但将其映射到子类?

最佳答案

我认为问题在于,在PermissableResource中定义了@ManyToOne关系,而在Resource类中定义了@OneToOne。尝试用@OneToMany替换后者。

09-12 23:56