-->
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: Problem with Multiple Insert
PostPosted: Mon Apr 30, 2007 7:23 am 
Newbie

Joined: Mon Apr 09, 2007 10:29 am
Posts: 14
Hi

I have to insert an object which have a list of another type objects. So i start a transaction and if all inserts are successful i commit else i rollback. The list of objects are independant objects and they have to be inserted before the main object. The problem is that when second object is inserted it gives the error "a different object with the same identifier value was already associated with the session: 0, of class:"
But i have to insert them in one transaction.

What mistake I am making or theer is a different way to do this??

Saurabh


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 30, 2007 9:30 am 
Senior
Senior

Joined: Sat Apr 21, 2007 11:01 pm
Posts: 144
Can you post some code and config.

kthx.

_________________
Everytime you get an answer to your question without giving credit; god kills a kitten. :(


Top
 Profile  
 
 Post subject: Re: Problem with Multiple Insert
PostPosted: Mon Apr 30, 2007 11:15 am 
Newbie

Joined: Mon Jan 29, 2007 2:03 am
Posts: 11
saurabhs_iitk wrote:
Hi

I have to insert an object which have a list of another type objects. So i start a transaction and if all inserts are successful i commit else i rollback. The list of objects are independant objects and they have to be inserted before the main object. The problem is that when second object is inserted it gives the error "a different object with the same identifier value was already associated with the session: 0, of class:"
But i have to insert them in one transaction.

What mistake I am making or theer is a different way to do this??

Saurabh



This should be done using sqlbulkcopy, not thru nHibernate


Top
 Profile  
 
 Post subject: Re: Problem with Multiple Insert - cheesy workaround
PostPosted: Tue May 01, 2007 10:32 am 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
saurabhs_iitk wrote:
The problem is that when second object is inserted it gives the error "a different object with the same identifier value was already associated with the session: 0, of class:"
But i have to insert them in one transaction.


Others may have a better solution for you, and I agree with Adam that seeing your mappings/classes would help, but if you get no love from other quarters, try this:

Create a util class global that provides a decrementing value, from -1. It only needs to be process local, but it certainly should be threadsafe. Not too bad, since you should be able to use interlocked decrement instead of full-on monitors ( lock ( object ) {...} ).

Now, in your child class ctor, assign the identifier value from this global instead of defaulting it to 0.

You will probably need an unsaved-value for SaveOrUpdate so cascades will be supportable (handy for P-C) so add a version column to your table/mapping/class, and use the unsaved-value there.

Ugly? Kinda, but it should solve your problem in lieu of an actual, you know, solution.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 02, 2007 7:13 am 
Newbie

Joined: Mon Apr 09, 2007 10:29 am
Posts: 14
The problem only comes when i am using SaveOrUpdate and if i use Save it works fine so i changed my logic and used Save.
I was thinking on the similar lines to provide an identifier value from a generator class in -ves if Save wouldn't have worked.
Thanx for the solutions

Saurabh


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.