我有一个带有复合键的实体,所以我使用了@Embeddable和@EmbeddedId注释。 Embeddable类看起来像这样:
@Embeddable
public class DitaAdminAccountSkillPK implements Serializable {
@ManyToOne
@JoinColumn(name = "admin_id")
private DitaAdmin admin;
@ManyToOne
@JoinColumn(name = "account_id")
private DitaAccount account;
//constructor, getters, setters...
}
使用它的实体:
@Entity
public class DitaAdminAccountSkill {
@EmbeddedId
private DitaAdminAccountSkillPK id;
//constructor, getters, setters...
}
现在我想将实体映射到另一个实体中:
@OneToMany(fetch = FetchType.LAZY, mappedBy = "id.admin")
private List<DitaAdminAccountSkill> accountSkills;
注意 mappedBy =“id.admin”使用 id 引用 DitaAdminAccountSkillPK 中的 admin 字段 DitaAdminAccountSkill 的字段。
这编译并运行得很好。但是,在eclipse中显示的错误显示: 在属性“accountSkills”中,“映射依据”值“id.admin”无法解析为目标实体上的属性。
请注意,这是 JPA问题,这意味着JPA方面正在抱怨。 现在,我知道我可以使用@IdClass,但我只是想知道为什么它认为它是一个错误。或者我可能做了一件非常糟糕的事情?
答案 0 :(得分:21)
根据JPA 2.0 specification:的第11.1.15节,不支持在嵌入式ID类中定义的关系映射。但是,此可能支持你正在使用的JPA实现,即使它没有得到标准本身的正式支持。
如果是这种情况,您可能希望在Window -> Preferences -> Java Persistence -> JPA -> Errors/Warnings -> Attributes -> Cannot resolve attribute name
下的Eclipse中关闭此验证。
答案 1 :(得分:7)
就我而言,在我将以下内容设置为Ignore
之前,问题仍未得到解决:
Project Facets > JPA > Errors/Warnings > Type > Mapped Java Class is a member class
答案 2 :(得分:3)
在尝试之前的任何解决方案之前,请先检查persistence.xml
并确保exclude-unlisted-classes
设置为true
,或者所有映射的类都列在persistence-unit
中
答案 3 :(得分:2)
以为我会发布我发现的符合JPA 2.0规范的解决方案,并且看起来功能相同。
首先,可以在此处找到JPA 2.0规范:JSR-000317 Persistence Specification for Eval 2.0 Eval。相关部分将是2.4.1“与派生身份相对应的主键”
以下是使用您指定的类的示例:
嵌入式ID类:
@Embeddable
public class DitaAdminAccountSkillPK implements Serializable {
//No further annotations are needed for the properties in the embedded Id.
//Needs to match the type of the id of your DitaAdmin object. I added 'Id' to the end of the property name to be more explicit.
//Making the assumption here that DitaAdmin has a simple Integer primary key.
private Integer adminId;
//Needs to match the type of the id of your DitaAccount object. I added 'Id' to the end of the property name to be more explicit.
//Making the assumption here that DitaAccount has a simple Integer primary key.
private Integer accountId;
//I'm adding a third property to the primary key as an example
private String accountName;
//constructor, getters, setters...
//hashCode() and equals() overrides
}
“依赖”实体类:
@Entity
public class DitaAdminAccountSkill {
@EmbeddedId
//Any overrides to simple Id properties should be handled with an attribute override
@AttributeOverride(name = "accountName", column = @Column(name = "account_name"))
private DitaAdminAccountSkillPK id;
//MapsId refers to the name of the property in the embedded Id
@MapsId("adminId")
@JoinColumn(name="admin_id")
@ManyToOne
private DitaAdmin admin;
@MapsId("accountId")
@JoinColumn(name="account_id")
@ManyToOne
private DitaAccount account;
//constructor, getters, setters...
}
“父级”实体类:
public class DitaAdmin {
@Id
private Integer id;
//...
//Now your mappedBy attribute can refer to the admin object defined on DitaAdminAccountSkill which is also part of the Primary Key
@OneToMany(fetch = FetchType.LAZY, mappedBy="admin")
private List<DitaAdminAccountSkill> accountSkills;
//...
}
答案 4 :(得分:1)
偏好设置 - &gt; Java持久性 - &gt; JPA - &gt;错误/警告 - &gt;属性 - &gt;嵌入式ID类不应包含关系映射:(忽略)