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

问题描述 投票:13回答:5

我有一个具有复合键的实体,所以我使用@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”,它使用DitaAdminAccountSkill的id字段引用DitaAdminAccountSkillPK中的admin字段。

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

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

java eclipse hibernate jpa composite-key
5个回答
21
投票

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

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


7
投票

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

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

3
投票

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


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;

    //...
}

1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.