-->
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.  [ 3 posts ] 
Author Message
 Post subject: session.lock(obj, LockMode.NONE) bug?
PostPosted: Wed Sep 10, 2003 12:01 pm 
not sure if this is by design or if it is a bug, but i'm 90% sure that this is what is going on. i have two transient objects in memory: A and B, each of which has a one-to-many relationship with a lazy loading collection C (which has been initialized on both instances). the same instance of the class in C is associated to both A and B. now, when i do:

session.lock(A, LockMode.NONE);

i see a bunch of:

Code:
11:52:55,388 DEBUG Cascades:326 - processing cascades for: com.sa.go.bto.Business
11:52:55,398 DEBUG Cascades:350 - cascading to collection: com.sa.go.bto.Business.categories
11:52:55,398 DEBUG Cascades:72 - cascading to lock()
11:52:55,398 DEBUG Cascades:259 - unsaved-value strategy NULL
11:52:55,398 DEBUG SessionImpl:1605 - reassociating transient instance: [com.sa.go.bto.Category#71]
11:52:55,398 DEBUG Cascades:72 - cascading to lock()


you can see that the transient instances in collection C are be cascaded to lock() as well - which is fine. problem is, i now do:

session.lock(B, LockMode.NONE);

and then issue a session.flush(), i get:

Code:
net.sf.hibernate.HibernateException: Another object was associated with this id (the object with the given id was already loaded): [com.sa.go.bto.Category#115]
   at net.sf.hibernate.impl.SessionImpl.doUpdate(SessionImpl.java:1301)
   at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1232)
   at net.sf.hibernate.engine.Cascades$5.cascade(Cascades.java:105)
   at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:280)
....


is this a bug in the cascading code for lock() calls?


Top
  
 
 Post subject:
PostPosted: Wed Sep 10, 2003 12:55 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
It looks extremely unlike a bug.


It looks like you have two different instances of Category, both with the identifier 115.

I suggest you get out your debugger and test my theory.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 11, 2003 5:00 pm 
Newbie

Joined: Tue Aug 26, 2003 9:45 am
Posts: 17
Location: Toronto, Canada
I think in Category you have not overridden equals() and hash() to work on the actual ID value. On this basis, if you have two instances in memory, Hibernate can't tell they are really the same entity. This can happen with a number of scenarios. For example, you cache an instance (transient) of Category, then later you load a graph of objects which loads another instance with the same id, then you attach your cached instance to somewhere on the object graph which is subject to cascade saving. When you save the graph, you would get this error. In your case, I think it's even simpler.

The solution is to provide a valid equals() and hash() method. But in that case, keep in mind that you are effectly hosed if you generate IDs of a sequence or other native mechanism, and need this object to be a member of a collection, since you will be chaning the key when the object is already in the collection. Bummer.

I wish Gavin would admit that the only clean way to resolve the last issue is for Hibernate itself to rehash the collections that the object is a member of :-) The only solution I know of without changing hibernate, is to make hash() always return the same value, thereby turning all hashtables into linear lists, but at least not violating Collection invariants.

Regards,
Colin


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