2011-07-15 9 views
0

を取り外すことなく、親オブジェクトは、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

答えて

0

あなたは子テーブルでFKの親フィールド上の非NULL制約を持っていない場合は、あなただけの子供をゼロにして保存するために親を設定することができます。休止状態は、プレーンゲッターとセッターを必要として、

Boolean removeChild(Child c) { 
    Boolean success = getChildren().remove(c); 
    if(success) c.setParent(null); 
    return success; 
} 

私はsetChildrenメソッドを上書きするには注意がしたい:

は、私は次のメソッドを記述します。

0

私はあなたが手動で設定する必要がありますだと思います削除された要素の親はnullになります。あなたのためにこれを行う休止状態のものは何もありません。 Better Beans Binding ProjectのObservable Collectionを使用すると、少し上手くいくことができます。次に、あなたのセッターを削除し、変更をすぐに反映させることができます。

public class Parent implements Serializable, ObservableListListener { 
    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; 
    } 
    void listElementPropertyChanged(ObservableList list, int index) { // Do Nothing } 
    void listElementReplaced(ObservableList list, int index, java.lang.Object oldElement) { 
     listElementAdded(list.get(index)); 
     listElementRemoved(oldElement); 
    } 
    void listElementsAdded(ObservableList list, int index, int length) { 
     for(int i = index; i < index + length; i++) { 
      ((Child)list.get(i)).setParent(this); 
     } 
    } 
    void listElementsRemoved(ObservableList list, int index, java.util.List oldElements) { 
     for(object element : oldElements) { 
      ((Child)list.get(i)).setParent(null); 
     } 
    } 
} 
関連する問題