[英]Hibernate Unidirectional OneToMany with composite key
我想框架Hibernate OneToMany關系,其中Parent具有一個復合主鍵,Child具有一個主鍵(由hibernate-auto生成)。 下面是我的工作示例代碼:
class Parent{
@EmbeddedId
private ParentPk parentPk;
@OneToMany( mappedBy="parent")
private List<ChildType1>;
@OneToMany( mappedBy="parent")
private List<ChildType2>;
@OneToMany( mappedBy="parent")
private List<ChildType3>;
//--setters and getters
}
@Embeddable
public class ParentPk {
private Long parentId;
private BigDecimal version;
//..setters and getters
}
class ChildType1{
@Id
private Long childId;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumns({ @JoinColumn(name = "parentId"),
@JoinColumn(name = "version") })
private Parent parent;
//..other fields and setters and getters
}
//--ChildType2 and ChildType3 like above
但是現在我想在上面將模型建模為OneToMany單向關系,即,子級不應引用父級(要在子級中省略Parent實例)。 可能嗎?
一個示例方法:
@Entity
class Parent {
@EmbeddedId
private ParentPk parentPk;
@OneToMany
@JoinColumns({
@JoinColumn(name = "parentId", referencedColumnName = "parentId"),
@JoinColumn(name = "version", referencedColumnName = "version")
})
private List<ChildType1> children1;
// exactly the same annotations as for children1
private List<ChildType2> children2;
// exactly the same annotations as for children1
private List<ChildType3> children3;
//..other fields and setters and getters
}
@Entity
class ChildType1 {
@Id
private Long childId;
//..other fields and setters and getters
}
//--ChildType2 and ChildType3 like above
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.