-->
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.  [ 8 posts ] 
Author Message
 Post subject: NonUniqueObjectException after call to Session.contains(...)
PostPosted: Wed Jun 29, 2005 11:32 am 
Newbie

Joined: Wed Jun 08, 2005 3:12 pm
Posts: 6
I have code that looks similar to the following:

Sesson session = ....; // get the session
User user = ...; // retrieve object from HttpSession
if (!session.contains(user))
{
session.lock(user, LockMode.NONE); // throws exception
}

How is it possible that I'm seeing a NonUniqueObjectException after checking via contains(...) that the instance is not already associated with the session?!?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 02, 2005 3:36 pm 
Newbie

Joined: Tue Nov 23, 2004 6:10 pm
Posts: 1
was there an answer to this? I am having the exact same issue, and cannot figure out why this is happening. Any help would be great.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 02, 2005 4:37 pm 
Regular
Regular

Joined: Thu May 26, 2005 2:08 pm
Posts: 99
I've been able to recreate this by doing the following:

Foo contains a Set of Bar. (bidirectional)

* Get a Foo from the session.
* Evict Foo.
* Get a Bar that is associated with Foo from the session. This gets Foo as well.
* Check if Foo is known to the session with session.contains(). It's not.
* Try to lock Foo. NonUniqueObjectException is thrown, giving the name and ID of Foo in the error message.


Top
 Profile  
 
 Post subject: Contains(Object)? Depends on what your meaning of is is.
PostPosted: Sat Sep 10, 2005 5:40 pm 
Newbie

Joined: Tue Jun 07, 2005 3:42 pm
Posts: 5
When you load Bar with the associated Foo, a new persistent Foo instance is created in the session cache. Session#contains() does not match a detached instance of Foo -- like the one that you evict()ed -- since contains() uses JVM == rather than a comparison of persistent identity.

The purpose of contains() is to indicate whether a particular Java object is "persistent", i.e. whether or not that exact object exists in the session cache.

Unfortunately there is no interface for what you want to test: Is there a persistent identity equivalent object in the cache? (PersistentContext#checkUniqueness is not exposed in Session.)

I think there is a use case for exposing that test in the interface: If your session throws an exception you're supposed to discard it, so there ought to be a way that you can test if an update() or lock() is allowed without incurring the corruption of your session.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 21, 2005 10:02 am 
Newbie

Joined: Tue Jul 12, 2005 5:17 am
Posts: 9
Is there is an answer to this question?
I.e. if there is a method to check if an hibernate session contains another object with class and identifier identical to that of a given one.

Thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 21, 2005 6:36 pm 
Newbie

Joined: Mon Jun 13, 2005 11:02 pm
Posts: 3
ttt


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 06, 2005 4:46 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
same problem ... did any of you get anywhere on this?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 28, 2006 12:41 pm 
Newbie

Joined: Thu Sep 28, 2006 12:27 pm
Posts: 13
When I had a similar problem just now I tried the following workaround:
Code:
tempTest = //Retrieve from HttpSession

Test newTempTest = (Test)sess.get(Test.class, tempTest.getTestId()); //Session.get() takes "Hibernate-Identity" into consideration?
               
if(newTempTest == null) //Not yet in the Session. Safe to lock
{
   sess.lock(tempTest, LockMode.NONE);
   newTempTest = tempTest;
}

That got rid of the NonUniqueObjectException and I couldn't spot any unwanted side-effects.


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