-->
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: Direct Copy of Entity
PostPosted: Tue Feb 26, 2008 6:15 pm 
Newbie

Joined: Sat Dec 08, 2007 9:22 am
Posts: 10
Hi All,

Very, very new to NHib so appologise in advance for stupidity. I need to diectly copy an entity based upon another one and persist.

I have looked at SaveOrUpdateCopy which seems to do the job except that it automatically assigns a new Id which I cannot use to write to the DB as SQL server is being used to generate the identifier thus I get the old.

"Cannot insert explicit value for identity column in table 'xxx' when IDENTITY_INSERT is set to OFF."

Additionally, if I manually reset the identifier field to null I get NHib throwing an exception telling me I have change this. Amy ideas how I go about this?

Chris.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 27, 2008 3:57 pm 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
The recommended way is to do the hard work and implement a Clone() method, then save the copy. For example:
Code:
Car car = session.Get<Car>(id);
Car carCopy = car.Clone();
session.Save(carCopy);

_________________
Karl Chu


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 28, 2008 2:00 pm 
Regular
Regular

Joined: Wed Jan 25, 2006 1:11 am
Posts: 118
Location: Copenhagen, Denmark
If i understand ýou correct, you want to save a new object to the database based on an old objects values. Assuming you have an unsaved-value on your native identity mapping, you should be able to do this:

Car car = session.Get<Car>(id);
session.Evict(car);
car.ID = 0; //or your unsaved-value
session.Save(car);


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 29, 2008 5:51 am 
Newbie

Joined: Sat Dec 08, 2007 9:22 am
Posts: 10
karlchu - Thanks for the reply, I didnt want to have to go down the route of implementing IClonable for all my entities ad there are a load of them and this is just a concept at the moment, so was looking for a "quick and easy" option.

jta - Cheers, I'm just getting to grips with NHib and didn't previously see the value in the Evict method, but it makes sense to me know. Thanks for the help.


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.