For example, we now have a parent object and a children's set in the parent object.
The children data is not very important. For convenience, when we modify the parent
1. Delete all children and add all new children.
This morning, when I was doing this, I encountered a problem, that is, when I updated the parent, I reported
Don't dereference a collection with cascade = "all-delete-Orphan"
Usually half an hour of Data Query and debugging, finally found a solution to the problem
At the beginning, my practice was as follows:
Parent = parentservice. findparentbyid (ID );
Parent. getchildren. Clear ();
Parent. setchildren (newchildren );
Parentservice. updateparent (parent );
In this case, an exception of don't dereference a collection with cascade = "all-delete-Orphan" will be reported.
Originally, for the children set of parent, it is a persistent set, which exists in the object pool of hibernate, through
After parent. setchildren (newchildren) is set, the reference of parent to children set has been referred to a set outside the object pool.
The method after querying the information is:
Parent = parentservice. findparentbyid (ID );
Parent. getchildren. Clear ();
Parent. getchildren. addall (newchildren );
Parentservice. updateparent (parent );
After several tests, the problem was solved.
HBM configuration is as follows:
<Set lazy = "true" name = "children" cascade = "All, delete-Orphan" inverse = "true">
<Key column = "parent_id"/>
<One-to-learn class = "child"/>
</Set>
In addition, Hibernate 3 has modified the options in cascade, And now it does not have the all-delete-orphan option, although hibernate
This option is still supported internally ......