I have a typical one-to-many relation parent -> children where children are a set. I wish to create another parent object whose children are copies of the children of the original. It appears that the only way to do this is to create a new parent object object, persist it, and ONE at a time create a copy of each child - perists it - and add it to the parent copy.
More explicitly
Code:
Transaction t = session.beginTransaction();
Parent copy = ParentFactory.newInstance(originalParent);
Iterator i = original.getChildren().iterator();
while(i.hasNext()){
Child child1 = (Child) i.next();
Child child2 = ChildFactory.newInstance(child1);
copy.addChild(child2);
session.saveOrUpdate(copy);
}
t.commit();
Is this correct? is there a simpler way or a more efficient way?
thank you