-->
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 between sessions
PostPosted: Thu Jun 24, 2004 12:55 pm 
Newbie

Joined: Mon Jun 21, 2004 11:58 pm
Posts: 5
Hibernate 2.1.4
JBoss 3.2.3

My hibernate.cfg doesn't have anything defined other than the datasource, the dialect and what objects to load, so any settings are whatever the hibernate "defaults" would be. And the object configs are what got generated from the middlegen plugin, so defaults are likewise whatever it spits out.

I'm seeing a weird behaviour that I don't understand in the code I'm working on.

I have a very simple struts Action that uses a HibernateUtil class pretty much exactly as outlined in the docs/tutorials to set up the ThreadLocal session object and etc. The action does basically this:

Code:
public ActionForward execute(blah...) {
   Session sess = HibernateUtil.getSession();

   User user;
   Role role;
   boolean foundUser;

   try {
      List users = sess.find("from User user where user.userid = ?", .....);
      // finds the user in the results and puts it in the "user" object
      // and set foundUser = true if it's there
   }
   catch(Exception e) {
      /// blah blah
   }

   if(foundUser && user.getRole() == null) {
      try {
         r = new Role();
         sess.load(r, "Generic User Role ID");
      }
      catch(Exception e) {
         // blah blah
      }
   }

   HibernateUtil.closeSession();

   // do some more stuff with my objects
}



If I load this page twice in a row, I get a NonUniqueObjectException the second (and consecutive) times I load the page on the attempt to load "role" and my "role" object is empty.

However, if I replace the part of the code that makes a new Role and calls load() to get the Role, with:

Code:
r = (Role)sess.load(Role.class, "Generic User Role ID");


then I no longer get the exception and my Role object is full of headly goodness as I want both the first and consecutive times I load the page.

Obviously it's because the Role() object already exists in the session from the previous time I instantiated a new object and loaded it with Hibernate. There are a couple of things I don't understand about this though:

1) If I'm closing the session, why is the object present in the new session object I grab from HibernateUtil? HibernateUtil calls sess.close(), which is documented as doing "cleanup", which we're assuming means it flushes objects out of any cache it might have and such. Obviously that's wrong because hibernate still knows about the object the next time the code gets called.

2) Is doing "new Object(); sess.load(object, "ID")" the "wrong" way to do this sort of thing? If so, why is that the method outlined in all the tutorials/examples? Should we go change it throughought the rest of the code we've already written to do it the second way that doesn't cause me exceptions?

Even granting my noob state with Hibernate, it is very surprising to me that such an obvious (from my perspective) and even recommended as per the documentation way of playing with Hibernate objects can so easily generate an exception that pretty much fails to do what I am asking it to do.

I'm obviously missing something, but I don't know what. The fact that even the other programmers with a lot more Hibernate experience working on this project didn't understand why my code broke so readily makes me think I'm missing something maybe not so obvious. We're presuming there's some further layer of caching that we don't know about/don't understand correctly.

Even just a pointer to a page that explains what's going on and why I'm doing things "wrong" would be appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 03, 2004 4:04 am 
Newbie

Joined: Fri Apr 30, 2004 7:12 am
Posts: 4
Hi there ... :-)

I just came across your post, and you may very well have saved my bacon!

I was having exactly the same problem; a NonUniqueObjectException between successive refreshes of the page.

I changed the session.load call, and the exception vanished (I had about six of them all over the app - we were on the verge of abandoning the Hibernate layer).

Following your example, I changed all the session.loads and the thing now works OK ... so thanks very much.... :-)

I am curious as to what causes this though.

Quote:
Even granting my noob state with Hibernate, it is very surprising to me that such an obvious (from my perspective) and even recommended as per the documentation way of playing with Hibernate objects can so easily generate an exception that pretty much fails to do what I am asking it to do.


That's my feeling on it. Is it a bug in Hibernate? If using load in this way, causes the session closure to fail so badly, then surely it would have been spotted elsewhere


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.