Session.merge() and Session.update() behave differently for cascade="all-delete-orphan" relationships
I have the following entity relationship setup.
Code:
class Parent
{
//inverse="true" cascade="all-delete-orphan" (hbm)
Set<Child> children;
}
Code:
class Child {
//many-to-one (hbm)
Parent parent;
//inverse="true" cascade="all-delete-orphan" (hbm)
Set<GrandChild> grandChildren;
}
Code:
class GrandChild {
//many-to-one
Child child;
String name;
}
My application load's the parent class instance and sends it to the UI in one transaction and does one of the following below before calling saveOrUpdate() in a different transaction.
>When updating a grand child's name and calling Session.saveOrUpdate(Parent) the child's name is updated due to cascade
>When adding a new grand child in the Child's set and calling
Session.saveOrUpdate(Patent) the new child is added due to cascade
>But When removing grand child element from the child and then doing a
Session.saveOrUpdate(Parent) doesn't remove the child in fact the version column of the that grandchild record is untouched,
Session.update(Parent) also doesn't remove the grand child.
but doing Session.merge(Parent) does remove the grand child
Is this correct behavior? I thought if "all-delete-orphan" option is given then it should cascade regardless of it being a merge() or update().
I am using Spring Framework's HibernateTemplate.merge(), HibernateTemplate.update(), HibernateTemplate.saveOrUpdate() functions.
Update: It looks like the issue is with the way i was updating the Set collections.
for session.saveOrUpdate() or session.update() to cascade properly you need to either use collection.remove() or collection.clear() followed by collection.set();
If you do just collection.set() it does not remove items from the collection.