A collection with cascade=“all-delete-orphan” was no longer referenced by the owning entity instance

后端 未结 5 1221
终归单人心
终归单人心 2021-02-01 07:26

In my application, a hibernate operation goes like this. The application updates a parent entity with new values from the request and deletes all the existing (previously insert

5条回答
  •  有刺的猬
    2021-02-01 08:20

    Your last snippet of Java code doesn't compile. I guess it looks like

    parent.getChilds().clear(); // note: you should name it children rather than childs
    parent.setChilds(someNewSetOfChildren):
    

    Don't do the last instruction. Instead of replacing the set by another one, clear the set and add the new children to the cleared set:

    parent.clearChildren();
    parent.addChildren(someNewSetOfChildren);
    

    where the methods are defined as:

    public void clearChildren() {
        this.children.clear();
    }
    
    public void addChildren(Collection children) {
        this.children.addAll(children);
    }
    

    The setChildren method should be removed completely, or it should be replaced with the following implementation:

    public void setChildren(Collection children) {
        this.children.clear();
        this.children.addAll(children);
    }
    

提交回复
热议问题