-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: How to efficiently copy collections?
PostPosted: Thu Mar 04, 2004 7:34 am 
Regular
Regular

Joined: Thu Aug 28, 2003 10:54 am
Posts: 67
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


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 04, 2004 7:59 am 
Newbie

Joined: Thu Nov 13, 2003 12:03 pm
Posts: 18
Location: France
I had the same case.
You can avoid the save on each child by using cascade mode "save-update".
The problem is that those two collections of children are not exactly identical since one is linked to the original parent and the other one to the dest parent.
So I had to go through all the copied children to reassign the new parent.
I am not sure that it helped you ;-)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 04, 2004 8:04 am 
Regular
Regular

Joined: Thu Aug 28, 2003 10:54 am
Posts: 67
thanks, i kinda figured it would have to be like this. I am not too sure assignment matters too much in my case as the relationship is not bidirectional, but its true that the child objects are not the same as the originals.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 05, 2004 4:42 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
teknokrat wrote:
but its true that the child objects are not the same as the originals.

The child objects are the same. Only the collection containing them must be different.

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.