表格:
FATHER_TABLE (PK CONF_ID, CONF_TYPE)
CONF_ID (NUMBER(2)),
CONF_TYPE(VARCHAR2(10),
... some other ininfluent columns
CHILD TABLE (PK CONF_ID, CONF_TYPE, CONF_DETAIL)
CONF_ID (NUMBER(2)),
CONF_TYPE(VARCHAR2(10),
CONF_DETAIL(NUMBER(2)),
... some other ininfluent columns
父亲表实体定义为:
public class FatherId implements Serializable {
private static final long serialVersionUID = 1L;
private Long confId;
private String confType;
//equals and hashcode
}
@Entity
@IdClass(FatherId.class)
@Table(name = "FATHER_TABLE")
public class Father {
@Id
@Column(name = "CONF_ID")
private Long confId;
@Id
@Column(name = "CONF_TYPE")
private String confType;
@OneToMany(mappedBy="father", fetch=FetchType.EAGER)
private Set<Child> = new HashSet<Child>
}
如何在假设的
FatherId
IdClass中或直接在ChildId
实体类中引用Child
? 最佳答案
这是一个“派生身份”,您可以这样映射Child
:
public class ChildId implements Serializable {
private Long confDetail; // matches the name of the attribute
private FatherId father; // matches name of attribute and type of Father PK
...
}
@Entity
@IdClass(ChildId.class)
@Table(name = "CHILD_TABLE")
public class Father {
@Id
@Column(name = "CONF_DETAIL")
private Long confDetail;
@Id
@JoinColumns({
@JoinColumn(name="CONF_ID", referencedColumnName="CONF_ID"),
@JoinColumn(name="CONF_TYPE", referencedColumnName="CONF_TYPE")
})
@ManyToOne
Father father;
}
JPA 2.1规范的第2.4.1节中讨论了派生身份(带有示例)。