使用@EmbeddedId进行映射时出现Eclipse错误

我有一个具有复合键的实体,所以我使用@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 accountSkills; 

注意mappedBy =“id.admin” ,它使用DitaAdminAccountSkillid字段引用DitaAdminAccountSkillPK中的 admin字段。

这编译并运行得很好。 但是,在eclipse中显示的错误显示: 在属性’accountSkills’中,“映射的”值’id.admin’无法解析为目标实体上的属性。

请注意,这是一个JPA问题 ,这意味着JPA方面正在抱怨。 现在,我知道我可以使用@IdClass,但我只是想知道它为什么认为它是一个错误。 或者我可能做了一些非常错误的事情?

根据JPA 2.0规范的第11.1.15节: 不支持在嵌入式id类中定义的关系映射。 但是,这可能会受到您正在使用的JPA实现的支持,即使它没有得到标准本身的正式支持。

如果是这种情况,您可能希望在Eclipse Window -> Preferences -> Java Persistence -> JPA -> Errors/Warnings -> Attributes -> Cannot resolve attribute name下关闭Eclipse中的validation。

在我的情况下,直到我将以下内容设置为Ignore才解决问题:

 Project Facets > JPA > Errors/Warnings > Type > Mapped Java Class is a member class 

在尝试任何先前的解决方案之前,首先检查您的persistence.xml并确保exclude-unlisted-classes设置为true或者所有映射的类都列在persistence-unit

以为我会发布我发现的符合JPA 2.0规范的解决方案,并且看起来function相同。

首先,可以在此处找到JPA 2.0规范: Eval 2.0 Eval的JSR-000317持久性规范 。 相关部分将是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 accountSkills; //... } 

首选项 – > Java持久性 – > JPA – >错误/警告 – >属性 – >嵌入式ID类不应包含关系映射:(忽略)