-->
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 when using second-level cache
PostPosted: Wed Jan 03, 2007 7:29 pm 
Beginner
Beginner

Joined: Mon Aug 01, 2005 3:51 pm
Posts: 22
Code:
public class B {
  private A a1;
  private A a2;

  public void setA1(A a) { this.a1 = a; }
  public void setA2(A a) { this.a2 = a; }
}


I fetch an instance of class A with id of 1 from a hibernate session then close the session. I store the fetched instance of class A to a variable named aFromFirstSession.

I open a new session and retrieve an instance of class B whose a1 references the same record (id of 1) that was fetched in my first hibernate session and a2 is null and close the session.

I then call setA2 passing aFromFirstSession, open a hibernate session and call saveOrUpdate on b.

This give a NonUniqueObjectException. This makes sense, because b is referencing two seperate instances that represent the same record (id of 1) since they were retrieved in seperate hibernate sessions.

I thought I could solve this by turning on the second-level cache for class A. With this approach, the instance will be the same across the multiple hibernate sessions because it is being retrieved from the second-level cache.

I still get the NonUniqueObjectException on the saveOrUpdate even though the log says that it was retrieved from the second-level cache.

Am I correct to think that if the instance is retrieved from the second-level cache I shouldn't get the NonUniqueObjectException in the above scenario?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 03, 2007 10:06 pm 
Regular
Regular

Joined: Thu Jul 29, 2004 11:55 pm
Posts: 75
You are correct in that the instance is recovered from the second level cache because it uses the id returned from the query. But your second assumption that it is the same object is incorrect. To stop you from corrupting the cached version or from having multiple threads update tha same object, it gives you different copies of the same object. If you have not overridden the equals and hashCode methods, it will think these are different objects and it will try to save the second version. The point of the second-level cache is not to minimise object creation, but unnecessary(duplicated) queries. Hope this answers your question.


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.