を取り外すことなく、親オブジェクトは、3人の子供のリストオブジェクトから始まり、子1を削除し、session.update(親)を呼び出して、どのようなHibernateマッピングの場合だろう削除された子テーブルを完全に削除せずに親PKを削除する必要がありますか?休止カスケード:子表のデリファレンス親PK子オブジェクト
私はまた、すべての子がそれと誤っカスケード削除任意の子エントリから私を防ぐ別の制約に割り当てている場合は、削除されてから任意の親を防ぐ制約を持っています。
私はでき単に(親)session.updateを呼び出すことが、私はトラブルの逆をやってを抱えていることで成功したカスケード更新の子供たち。
public class Parent implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private List<Child> children;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@OneToMany(mappedBy = "parent")
@org.hibernate.annotations.Cascade(
value = {org.hibernate.annotations.CascadeType.SAVE_UPDATE})
public List<Child> getChildren() {
return children;
}
public void setChildren(List<Child> children){
this.children = children;
for(Child child : children) {
child.setParent(this);
}
}
//----------------- Would I need something like this? ---------------
//----------------- Or does hibernate have a better way? ---------------
public void setChildren(List<Child> children){
for(Child child : this.children)
if (!children.contains(child)) {
child.setParent(null);
}
}
this.children = children;
for(Child child : children) {
child.setParent(this);
}
}
//----------------------------------------------------------------
}
public class Child implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private Parent parent;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@ManyToOne
public Parent getParent() {
return parent;
}
public void setParent(Parent parent) {
this.parent = parent;
}
}
このソリューションは似ていますが、私が代わりに私がリストから子供を取り除くよ、親の削除はないよ:
hibernate cascade - update child to null