-->
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: Two objects reference a 3rd - Problems when loading 2nd
PostPosted: Mon Sep 22, 2008 11:32 pm 
Newbie

Joined: Wed Sep 10, 2008 2:02 pm
Posts: 14
Location: NJ
I have instances of classes A and C that both reference the same persistent entity, an instance of class B. A happens to hold a collection of instances of class C (lazily loaded). The instance of class A gets loaded along with its reference to the instance of class B in one session and returned to a web-tier client for manipulation. Subsequently, the application decides that it wants to load the collection of instances of class C owned by the instance of class A. In the ejb-tier, in order to make the instance of class A's collection of instances of class C available, I get the current session, lock the instance of class A, then call Hibernate.initialize on the collection of instances of class C owned by the instance of class A. When the session is closed I get an exception:
Caused by: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session
The offending object is an instance of class B, the one referenced by the instance of class A and the only instance of class C in A's collection of C instances.
I'm surprised that this is happening. I would have thought that somehow Hibernate would understand that these two references are to the same object and that I would not have references to two different instances, but two references to the same instance. I thought that this would happen since I have associated the first instance with the session and asked Hibernate to load the object that also has a reference to that same object.
Can anyone give me an idea of how I might get around this problem?


Top
 Profile  
 
 Post subject: I found the solution, but I don't understand
PostPosted: Mon Sep 22, 2008 11:44 pm 
Newbie

Joined: Wed Sep 10, 2008 2:02 pm
Posts: 14
Location: NJ
I found that it works if I update the instance of A before initializing the collection of instances of C, but I don't understand why lock(xxx, LockMode.NONE) doesn't have a similar affect. I know I haven't made any changes to the state of A that need to be persisted, so it seems a waste to have to execute an update to the dbms.


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.