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.