Hibernate PersistentSet remove () operation does not work

I have Set in my parent entity as shown below:

Class Parent {
 @OneToMany(mappedBy = parent, cascade = CasacadeType.ALL)
 Set<Child> children;
}

Class Child {
 @Column(nullable=false)
 @ManyToOne
 Parent parent;
}

Now, if I perform the remove () operation in Set for one of its elements, it is not actually deleted.

+6
source share
3 answers

Your display should look like this:

public class Parent { 
    @OneToMany(mappedBy = parent, cascade = CasacadeType.ALL, orphanRemoval = true) 
    private Set<Child> children = new HashSet<>();

    public void removeChild(Child child) {
        children.remove(child);
        child.setParent(null);
    }
}

public class Child {
    @ManyToOne
    private Parent parent;
}

As explained in this article , since you have bidirectional communication, you must synchronize both sides.

Therefore, it is advisable to call:

parent.removeChild(child);

Therefore, it removeChildis going to draw Childfrom children Setand also establish for Child parentassociation with null.

+7

, remove setParent null, db. , . "hibernate set remove not working" , hibernate truth: , hashcode equals. , , removeAll() . removeAll, . :

List childList = new ArrayList();
childList.add(child);
parent.removeAll(childList);
child.setParent(null);
+2

, hashCode() .

. " equals hashCode" equals hashCode JPA ( ).

, . , , , hashCode . , Set .

- hashCode(). .

0

All Articles