I need to support undo operations in my app, and am in the situation where I have a big tree of Hibernate objects that have been saved/flushed but need to be reverted to their state right before the save/flush (using long-running sessions).
I've been doing:
Code:
session.save(object);
session.flush();
...
session.delete(object);
object.setId(null);
session.flush();
...
session.save(object);
session.flush();
which works, except when sets are involved. In that case, I have to get very dirty and do the following for the undo logic:
Code:
...
List<ChildObject> children = new ArrayList<ChildObject)();
for(ChildObject child: object.getChildren())
children.add(child);
object.setChildren(new HashSet<ChildObject>(0));
session.delete(object);
object.setId(null);
session.flush();
for(ChildObject child: children)
object.getChildren().add(child);
...
Obviously it's a huge pain to do, especially when a lot of objects are involved. Is there a better solution?