关于Eclipse的测绘误差@EmbeddedId(Eclipse error on mapping

2019-07-31 04:24发布

我有一个复合键的实体,所以我使用@Embeddable和@EmbeddedId注解。 嵌入式类看起来是这样的:

@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”使用DitaAdminAccountSkill的ID字段,其指的是管理字段中DitaAdminAccountSkillPK。

这编译和运行就好了。 然而,在日食出现,说显示错误: 在属性“accountSkills”,值“id.admin”“的映射”不能被解析到目标实体的属性。

请注意,这是一个JPA问题意味着JPA小抱怨。 现在,我知道我可以使用@IdClass代替,但我只是想知道为什么它认为它的错误。 或者,也许我做一些可怕的错误?

Answer 1:

根据第11.1.15 JPA 2.0规范 : 嵌入式ID类中定义的关系的映射不被支持。 然而,这可能是由你使用JPA实现支持 ,即使它不是正式的标准本身的支持。

如果这里的话,可能要在关闭验证,这在Eclipse Window -> Preferences -> Java Persistence -> JPA -> Errors/Warnings -> Attributes -> Cannot resolve attribute name



Answer 2:

就我而言,不解决这个问题,直到我设置为以下Ignore

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


Answer 3:

我以为我会后,我发现这是符合JPA 2.0规范,并且似乎工作方式相同的解决方案。

:首先,JPA 2.0规范可以在这里找到JSR-000317持久性规范评估和演示2.0评估和演示 。 相关的部分将是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;

    //...
}


Answer 4:

尝试任何以前的解决方案之前,先要检查自己persistence.xml ,并确保无论是exclude-unlisted-classes设置为true ,或者你所有映射类的中列出的persistence-unit



Answer 5:

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



文章来源: Eclipse error on mapping with @EmbeddedId