Java 使用@EmbeddedId映射时发生Eclipse错误
我有一个具有复合键的实体,所以我使用@Embeddedable和@EmbeddedId注释。 可嵌入类如下所示:Java 使用@EmbeddedId映射时发生Eclipse错误,java,eclipse,hibernate,jpa,composite-key,Java,Eclipse,Hibernate,Jpa,Composite Key,我有一个具有复合键的实体,所以我使用@Embeddedable和@EmbeddedId注释。 可嵌入类如下所示: @Embeddable public class DitaAdminAccountSkillPK implements Serializable { @ManyToOne @JoinColumn(name = "admin_id") private DitaAdmin admin; @ManyToOne @JoinColumn(name = "account_
@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...
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "id.admin")
private List<DitaAdminAccountSkill> accountSkills;
以及使用它的实体:
@Entity
public class DitaAdminAccountSkill {
@EmbeddedId
private DitaAdminAccountSkillPK id;
//constructor, getters, setters...
}
现在我想在另一个实体中映射该实体,如下所示:
@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...
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "id.admin")
private List<DitaAdminAccountSkill> accountSkills;
@OneToMany(fetch=FetchType.LAZY,mappedBy=“id.admin”)
私人清单会计技能;
注意mappedBy=“id.admin”,它使用DitaAdminAccountSkill的id字段引用DitaAdminAccountSkillPK中的admin字段
这可以编译并运行得很好。但是,在eclipse中会显示一个错误,该错误表示:
在属性“accountSkills”中,“mapped by”值“id.admin”无法解析为目标实体上的属性
请注意,这是一个JPA问题,意味着JPA方面正在抱怨。
现在,我知道我可以用@IdClass来代替,但我只是想知道为什么它认为这是一个错误。或者我做错了什么?根据的第11.1.15节:不支持在嵌入式id类中定义的关系映射。但是,您正在使用的JPA实现可能支持这个,即使标准本身没有正式支持它
如果是这样的话,您可能希望在Eclipse中的
窗口->首选项->Java持久性->JPA->错误/警告->属性->无法解析属性名-/code>下关闭对此的验证。我想我会发布我找到的解决方案,该解决方案符合JPA 2.0规范,并且似乎以相同的方式运行
首先,可以在以下位置找到JPA2.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;
//...
}
公共类DitaAdmin{
@身份证
私有整数id;
//...
//现在,mappedBy属性可以引用DitaAdminAccountSkill上定义的管理对象,它也是主键的一部分
@OneToMany(fetch=FetchType.LAZY,mappedBy=“admin”)
私人清单会计技能;
//...
}
在我的例子中,直到我将以下设置为“忽略”
,问题才得以解决:
Project Facets > JPA > Errors/Warnings > Type > Mapped Java Class is a member class
在尝试之前的任何解决方案之前,首先检查您的
persistence.xml
,并确保exclude unlisted class
设置为true
,或者您的所有映射类都列在持久化单元中
首选项->Java持久化->JPA->Errors/warning->Attribute->嵌入式ID类不应包含关系映射:(忽略)有没有JPA2.0规范支持的方法来实现这一点?回答了我自己的问题。JPA2.0规范(已找到)的第2.4.1.3节列出了许多非常有用的示例。我的解决方案是使用@EmbeddedID
和@MapsId
。找到了JPA2.0友好的解决方案:使用这两个选项(mappedBy=“id.admin”或mappedBy=“admin”)。。当我尝试映射“非惰性集合”时,我将得到以下错误。错误:resql.util.PSQLException:错误:列workflowst0\u0.stepseqno不存在。请看。提前谢谢。