Hey,
Yes cloning would work, but that means hibernate will have to delete and reinsert C and all its descendants.
I found a solution!
For A->B->C, suppose you delete B:
Code:
[begin transaction]
// optimization, if we know theres no descendants, we dont have to flush and refresh
if ( B.getChildCount() > 0 ) {
for each child of B {
A.add( child );
}
/* the flush will update all children of C to now point to A (update instead of insert since the entities are already persistent) */
session.flush();
// we refresh B since it dont have children anymore
session.refresh( B );
// we can now remove B, it has no more descendants
A.removeChild( B );
[commit transaction]
}
The in-memory domain model, the persistence store and the hibernate cache are all synchronized and reflects what is wanted:
A->C
The only problem is that I can't put this piece of code in the domain entity. Even if that's domain logic, since I access hibernate (the persistence layer). I would be coupling the domain model (pojos) with the persitence layer. So I created a command that take care of this.