-->
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: Using NHibernate to Duplicate an Object
PostPosted: Mon Jan 15, 2007 6:22 am 
Newbie

Joined: Tue Oct 24, 2006 2:51 pm
Posts: 19
NH 1.2.beta2
I was thinking if will be possibile to use NHibernate ORM layer to fetch a object with collections and many-to-one mappings, and then store the fetched object with a different Id, thus making a "copy and paste" operation.

I've not experimented, would be good to know if this could be a fine idea, eg. let's think about a User object with a Roles Collection: if i want clone the user admin

User u = es.Get<User>("admin);

u.Id = "AdminSecond";

es.SaveOrUpdate(), es.Save() or something different? How NHibernate could change the referenced keys accordingly?

thanks for any reply


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 17, 2007 5:42 am 
Newbie

Joined: Tue Oct 24, 2006 2:51 pm
Posts: 19
Any Idea?
I've evaluated the possibility to Evict() the object, chenge the id in the object tree and SaveOrUpdate() the transient copy

No one has the same behiavour to manage?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 17, 2007 8:31 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
what about implementing ICloneable? Then you could simply do something like:

User masterUser = es.Get<User>("admin");
User clonedUser = masterUser.Clone();
se.SaveOrUpdate(clonedUser);

The overridden ICloneable.Clone() method would implement a customized form of a deep copy without copying the ID field.

just off the top of my head...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 18, 2007 1:40 am 
Regular
Regular

Joined: Tue Feb 21, 2006 9:50 am
Posts: 107
an easy way to clone a object is to serialize it to a stream and then deserialize it. From our BaseModel:

Code:
private object cloneBySerialization()
{
  Object result;
  MemoryStream buffer = new MemoryStream();
  BinaryFormatter formatter = new BinaryFormatter();
  formatter.Serialize(buffer, this);
  buffer.Position = 0;
  result = formatter.Deserialize(buffer);
  return result;
}


You just have to ensure that your objects are seializable. In most cases using the [Serializable] attribute on class level will fit.

Regards
Klaus


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.