I have a parent child relationship:
Parent --* Child
* Parent cascades all to Child
* class Parent and collection Parent.children are in a process level cache
I want to be able to delete either a Parent -- with cascades to all it's children -- or a Child by itself.
Deleting the Parent is easy:
Code:
session.delete(parent);
This is how I was trying to delete the child:
Code:
session.delete(child);
// in class Child:
public boolean onDelete( Session session ) {
parent.getChildren().remove(this);
}
This code works for deleting all traces of the child ... but breaks the first code example of deleting the parent. This is because modifying the children Collection throws a ConcurrentModificationException since Hibernate is also iterating over the children, processing the cascades.
Is there any way to handle this elegantly? I know that I can create an onDelete() method in Parent that clears the parent of each child but this is ugly since the whole point of cascades is that they are declarative.