-->
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.  [ 5 posts ] 
Author Message
 Post subject: How can I duplicate an object?
PostPosted: Wed Jan 31, 2007 3:58 pm 
Senior
Senior

Joined: Fri Jan 13, 2006 2:50 pm
Posts: 123
Location: Blumenau / SC / Brasil
Hi.

I'm using NH 1.2.0 Alpha and .NET 2.0 and would like to know if it's possible to duplicate an object.

For example:

Code:
Cat c1 = Session.Get(1);
c1.Id = GetNextId(typeof(Cat));
Session.Save(c1);
Session.Flush(); // I've done the tests using Flush() and Transactions


When I use the code above, NH does not save c1. I think NH thinks that c1 alread is saved, so doesn't save...

Is there any way to duplicate an object?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 01, 2007 12:35 am 
Senior
Senior

Joined: Mon Aug 21, 2006 9:18 am
Posts: 179
Try evicting c1 before changing the identifier using Session.Evict(c1). Personally , I avoid this kind of thing by implementing ICloneable or serializeing the object to a file stream sans Id.

_________________
If this helped...please remember to rate it!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 01, 2007 7:45 am 
Senior
Senior

Joined: Fri Jan 13, 2006 2:50 pm
Posts: 123
Location: Blumenau / SC / Brasil
Thanks guy! :)

Just a question: why you avoid Evict?

Bye


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 02, 2007 4:10 am 
Senior
Senior

Joined: Mon Aug 21, 2006 9:18 am
Posts: 179
It's not Session.Evict I avoid...that is useful for the rare times I need to make a persisted object detached.
I just meant that when making a copy of an object I think it is more sensible to do a clone since it avoids any dependency on my persistence to perform that role...It's more intention-revealing. ICloneable exists for that purpose.
Just a matter of preference really.


MIKE

_________________
If this helped...please remember to rate it!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 02, 2007 6:57 am 
Senior
Senior

Joined: Fri Jan 13, 2006 2:50 pm
Posts: 123
Location: Blumenau / SC / Brasil
mnichols wrote:
It's not Session.Evict I avoid...that is useful for the rare times I need to make a persisted object detached.
I just meant that when making a copy of an object I think it is more sensible to do a clone since it avoids any dependency on my persistence to perform that role...It's more intention-revealing. ICloneable exists for that purpose.
Just a matter of preference really.


MIKE


ok, thanks anyway ;)


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