-->
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.  [ 2 posts ] 
Author Message
 Post subject: NonUniqueObjectException
PostPosted: Thu Jan 13, 2005 11:52 am 
Newbie

Joined: Thu Jan 13, 2005 11:41 am
Posts: 3
Hibernate version: hibernate2

Hi,

For my application I need to make some huge calculations. The problem is that I need to load objects out of my database. It is possible that I need to load 2 time the same object . When I do this I get NonUniqueObjectException exception.

Is there a way around this...

Thx in adv,
Pieter


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 13, 2005 4:58 pm 
Regular
Regular

Joined: Tue Jun 22, 2004 8:01 pm
Posts: 106
Location: PowderTown, Utah, USA
You are only allowed to have one instance of an entity object in your session at one time. The error you experienced occurs when you attempt to associate an entity from a (possibly earlier) hibernate session with another (later) session. (You associate an object with a session by using Session.lock() or Session.update()). If the object is already loaded in the session, you will get the exception you mentioned.

From the API documentation (in case you haven't read it and it sounds like you haven't):
Quote:
NonUniqueObjectException: This exception is thrown when an operation would break session-scoped identity. This occurs if the user tries to associate two different instances of the same Java class with a particular identifier, in the scope of a single Session.


The solution is quite simple. If you want to use the same object over and over with multiple sessions, or even if you want to re-load it and then keep it around, simply do a Session.evict() before you lock() or update(). If you want to simply save the object, you can do a saveOrUpdateCopy() which will work with a detached object.

Read up on detached objects. Read the FAQs too.

BTW, why on earth do you need to load an object multiple times in one operation anyway? An entity can only be in a single session, so I guess I don't get it.


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