当child 有复合PK 时,如何在parent 实体中定义@OneToMany?
How to define @OneToMany in parent entity when child has composite PK?
我的 Parent
class 有两个 child class:Child
和 ParentHobby
。 Child class 有一个单一的 PK,@OneToMany
映射在它上面。问题是不知道怎么映射到Parent爱好class,有复合PK
Parent:
//this works
@OneToMany(cascade = CascadeType.ALL, mappedBy = "parent", fetch = FetchType.EAGER)
private List<Child> childList;
//this DOES NOT work
@OneToMany(cascade = CascadeType.ALL, mappedBy = "parent", fetch = FetchType.EAGER)
private List<ParentHobby> hobbyList;
Child:
@Entity
@Table(name="CHILD")
public class Child {
@Id
@SequenceGenerator(name="CHILD_SEQ", sequenceName="CHILD_DB_SEQ", allocationSize = 1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="CHILD_SEQ")
@Column(name="CHILD_ID")
private long childID;
@JoinColumn(name = "PARENT_ID", referencedColumnName = "PARENT_ID", insertable = true, updatable = true)
@ManyToOne(optional = true)
private Parent parent;
Parent爱好:
@实体
@Table(名字="PARENT_HOBBY")
public class Parent爱好{
@EmbeddedId
private ParentHobbyPK id;
Parent爱好PK:
@Embeddable
public class ParentHobbyPK {
@JoinColumn(name = "PARENT_ID", referencedColumnName = "PARENT_ID", insertable = true, updatable = true)
@ManyToOne(optional = true)
private Parent parent;
@Column(name="HOBBY_ID")
private String hobbyID;
我在编译时得到的异常是:
mappedBy reference an unknown target entity property: ParentHobby.parent in Parent.hobbyList
当 child 具有复合主键时,如何在 parent 实体中定义 @OneToMany 关系?
类似:
@OneToMany relationship with Composite key
Hibernate Entity mapping when Foreign key is part of the composite primary key?
JPA composite key @OneToMany
您需要使用派生身份。
ParentHobbyPK
应该是这样的:
@Embeddable
public class ParentHobbyPK {
@Column(name="HOBBY_ID")
private String hobbyID;
private long parentID; // corresponds to the PK type of Parent
}
ParentHobby
应该是这样的(重要的是 @MapsId
注释):
@Entity
@Table(name="PARENT_HOBBY")
public class ParentHobby {
@EmbeddedId
private ParentHobbyPK id;
@MapsId("parentID") // maps parentID attribute of the embedded ID
@ManyToOne(optional = true)
@JoinColumn(name = "PARENT_ID", referencedColumnName = "PARENT_ID", insertable = true, updatable = true)
private Parent parent;
...
}
派生标识在 JPA 2.1 规范的第 2.4.1 节中进行了讨论。
我的 Parent
class 有两个 child class:Child
和 ParentHobby
。 Child class 有一个单一的 PK,@OneToMany
映射在它上面。问题是不知道怎么映射到Parent爱好class,有复合PK
Parent:
//this works
@OneToMany(cascade = CascadeType.ALL, mappedBy = "parent", fetch = FetchType.EAGER)
private List<Child> childList;
//this DOES NOT work
@OneToMany(cascade = CascadeType.ALL, mappedBy = "parent", fetch = FetchType.EAGER)
private List<ParentHobby> hobbyList;
Child:
@Entity
@Table(name="CHILD")
public class Child {
@Id
@SequenceGenerator(name="CHILD_SEQ", sequenceName="CHILD_DB_SEQ", allocationSize = 1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="CHILD_SEQ")
@Column(name="CHILD_ID")
private long childID;
@JoinColumn(name = "PARENT_ID", referencedColumnName = "PARENT_ID", insertable = true, updatable = true)
@ManyToOne(optional = true)
private Parent parent;
Parent爱好:
@实体 @Table(名字="PARENT_HOBBY") public class Parent爱好{
@EmbeddedId
private ParentHobbyPK id;
Parent爱好PK:
@Embeddable
public class ParentHobbyPK {
@JoinColumn(name = "PARENT_ID", referencedColumnName = "PARENT_ID", insertable = true, updatable = true)
@ManyToOne(optional = true)
private Parent parent;
@Column(name="HOBBY_ID")
private String hobbyID;
我在编译时得到的异常是:
mappedBy reference an unknown target entity property: ParentHobby.parent in Parent.hobbyList
当 child 具有复合主键时,如何在 parent 实体中定义 @OneToMany 关系?
类似:
@OneToMany relationship with Composite key
Hibernate Entity mapping when Foreign key is part of the composite primary key?
JPA composite key @OneToMany
您需要使用派生身份。
ParentHobbyPK
应该是这样的:
@Embeddable
public class ParentHobbyPK {
@Column(name="HOBBY_ID")
private String hobbyID;
private long parentID; // corresponds to the PK type of Parent
}
ParentHobby
应该是这样的(重要的是 @MapsId
注释):
@Entity
@Table(name="PARENT_HOBBY")
public class ParentHobby {
@EmbeddedId
private ParentHobbyPK id;
@MapsId("parentID") // maps parentID attribute of the embedded ID
@ManyToOne(optional = true)
@JoinColumn(name = "PARENT_ID", referencedColumnName = "PARENT_ID", insertable = true, updatable = true)
private Parent parent;
...
}
派生标识在 JPA 2.1 规范的第 2.4.1 节中进行了讨论。