[英]Unidirectional OneToMany with composite children key, not propagating parent id
我有以下2個實體:
@Entity
@Table(name = "PARENT")
public class ParentEntity{
@Id
@Column(name = "PARENT_ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "dtvSeq")
@SequenceGenerator(name = "dtvSeq", sequenceName = "PARENT_ID_SEQ")
private Long parentId;
@OneToMany(mappedBy = "childEntityPk.parentId", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
Collection<ChildEntity> childrenEntities;
//getters and setters ommited
}
子實體具有組成的主鍵,部分引用Parent_id(fk):
@Entity
@Table(name = "CHILDREN")
public class ChildEntity {
@EmbeddedId
private ChildEntityPk childEntityPk;
//other fields
//getter and setters ommited
@Embeddable
private class ChildEntityPk implements Serializable{
long parentId;
String name;
}
}
它完美地用於獲取數據。 但是,如果我想一次性保留ParentEntity及其子實體,則會從Oracle那里得到一個例外。
拋出此異常是因為休眠狀態未更新childEntity.childEntityPk.parentId並試圖插入null。
我也玩過@JoinColumn,但沒有成功。
我不想有雙向映射。
您有addChild()方法還是只是更新集合? 我建議
public void addChild(ChildEntity child) {
child.setChildEntityPk(this.parentId, "name");
childrenEntities.add(child);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.