14

我有一个带有复合键的实体,所以我使用 @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"使用DitaAdminAccountSkillid字段引用DitaAdminAccountSkillPK中的admin字段。

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

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

4

5 回答 5

25

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

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

于 2012-08-24T11:34:15.313 回答
7

就我而言,直到我将以下设置为Ignore

Project Facets > JPA > Errors/Warnings > Type > Mapped Java Class is a member class
于 2014-10-24T02:15:04.210 回答
4

以为我会发布我发现的符合 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;

    //...
}
于 2013-01-22T21:22:29.690 回答
3

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

于 2015-03-16T20:59:38.760 回答
1

Preferences -> Java Persistence -> JPA -> Errors/Warnings -> Attribute -> Embedded ID classes 不应该包含关系映射:(忽略)

于 2015-09-25T14:21:01.017 回答