-->
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.  [ 4 posts ] 
Author Message
 Post subject: Hibernate and HttpSession
PostPosted: Wed Jun 18, 2008 3:28 am 
Newbie

Joined: Tue Jun 17, 2008 10:35 am
Posts: 3
Hello,

I am new to Hibernate and have to finish and application started with Hibernate 3, Java 1.5 and Tomvat 6.

My problem is that I have an error when I save my object
Code:
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: .......


I think the error is due to the fact that the object (hibernate result) is store in the HTTPSession. After this object is modified and save with the "saveorupdate" method (i get the error here).

Is is the good method the work with Hibernate ?

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 19, 2008 9:35 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Yes, the good old HttpSession can play havoc with your entities.

Basically, the problem is that you have two JavaBean instances that represent the same data. One is loaded, I'm guessing by Hibernate, then you put it into the HttpSession, you pull it out of the HttpSession, at which point it is given a new memory location on the JVM, and as such, Hibernate thinks the object is different from the one pulled out of the HibernateSession. When you go to save the one pulled out of the HttpSession, Hibernate thinks its a different object pointing at the same data, and it gets upset.

One way to avoid this might be to override the .equals and .hashcode methods. A better way?

Just put the primary key of the object in the HttpSession, rather than the object. Then, when you need the object, pull it out of the Hibernate Session and update the values of the object as necessary. Not only will this eliminate Session bloat and take advantage of the POJO cached in the Hibernate Session's first level cache.

Take a look at this tutorial on How Hibernate3 Works for more information on persistent objects, detached objects and the NonUniqueObject Exception that you are getting.

http://www.hiberbook.com/HiberBookWeb/learn.jsp?tutorial=07howhibernateworks

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 20, 2008 9:58 am 
Newbie

Joined: Tue Jun 17, 2008 10:35 am
Posts: 3
Ok, that's what i have done and it's ok now.
Thanks

Lionel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 23, 2008 10:09 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Great! I love it when a Hibernate plan comes together. :)

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


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