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

use*_*058 13 java eclipse hibernate jpa composite-key

我有一个具有复合键的实体,所以我使用@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...
}
Run Code Online (Sandbox Code Playgroud)

以及使用它的实体:

@Entity
public class DitaAdminAccountSkill {

  @EmbeddedId
  private DitaAdminAccountSkillPK id;

  //constructor, getters, setters...
}
Run Code Online (Sandbox Code Playgroud)

现在我想在另一个实体中映射实体,如下所示:

@OneToMany(fetch = FetchType.LAZY, mappedBy = "id.admin")
private List<DitaAdminAccountSkill> accountSkills;
Run Code Online (Sandbox Code Playgroud)

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

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

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

小智 22

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

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


Cod*_*Med 7

在我的情况下,直到我将以下内容设置为Ignore:

Project Facets > JPA > Errors/Warnings > Type > Mapped Java Class is a member class
Run Code Online (Sandbox Code Playgroud)


FGr*_*reg 5

我想发布我发现的符合 JPA 2.0 规范并且看起来功能相同的解决方案。

首先,JPA 2.0 规范可以在这里找到:JSR-000317 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
}
Run Code Online (Sandbox Code Playgroud)

“依赖”实体类:

@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...
}
Run Code Online (Sandbox Code Playgroud)

“父”实体类:

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;

    //...
}
Run Code Online (Sandbox Code Playgroud)