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.  [ 3 posts ] 
Author Message
 Post subject: Insert cloning
PostPosted: Fri Nov 23, 2007 7:36 am 
Beginner
Beginner

Joined: Thu Oct 25, 2007 3:45 am
Posts: 22
I am trying hard to do something that looks quite simple. I have an object in the database (which has own collections and so on...). I need to load it (and I do it by id) and than I need to insert its cloning in the database (as completely new object).
I clone the object (by overwriting the Clone() function) and I zeros its id (and ids to its collections objects) - so hibernate "understands" it is a new object. But here come my troubles - hibernate really inserts the new object (the cloning) but makes also and some updates to the original object (its collections in fact - cascade update I think). For inserting of the new object I use the hibernate session function Save() (tried as well SaveOrUpdate(), SaveOrUpdateCopy()).
I can't see how to tell hibernate - insert the clone and don't tuch the orginal object.
Any help will be highly appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 23, 2007 5:15 pm 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
When you clone the object, make sure you create a new collection for the new object. That is, do this:
Code:
clone.Children = new List<Child>(this.Children);

and not this:
Code:
clone.Children = this.Children;

If each of the child also needs to be cloned, make sure you do that too:
Code:
clone.Children = new List<Child>();
foreach (Child c in this.Children) {
    clone.Children.Add(c.Clone());
}

_________________
Karl Chu


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 26, 2007 3:43 am 
Beginner
Beginner

Joined: Thu Oct 25, 2007 3:45 am
Posts: 22
Thank you!
I really had some problems with cloning...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.