Hello,
I'm new to Hibernate so this might be a stupid question, but anyway...
I'm wondering wether or not Hibernate transaction rollback affects persistent objects? Here is a scenario that illustrates my question:
Say I have a fat client that obtains a User object from the database when the user logs into the tool. This User object would be kept around for the duration of the user session with the tool in some globally accessible spot. When the user logs in you would have some code like this:
Code:
Session session=...
Query findUserQuery=session.getNamedQuery("findUserQuery");
findUserQuery.setString("name", name);
findUserQuery.setString("pw", pw);
User user=(User)findUserQuery.uniqueResult();
if (user!=null) {
GlobalSpot.put("user", user);
}
else {
//generate some error
}
Now suppose that the tool has a preferences window somewhere that allows the user to update some user prefs, which are stored in the user object. When the changes are applied, we execute some code like this:
Code:
Session session=...
Transaction tx=null;
try {
tx=session.beginTransaction();
User user=GlobalSpot.get("user");
user.setPrefs(newPrefs);
session.saveOrUpdateCopy(user);
tx.commit();
}
catch (Exception e) {
tx.rollback();
}
Suppose now that the saveOrUpdate() call throws an exception. As a result the transaction will be rolled back and the DB will still have the old preferences. However, the user object will not have been rolled back and has the new, unsaved preferences!
Is this normal behaviour? To me this seems a bit strange because that way I still have to do a lot of stuff to do a rollback on the object. This seems like something the OR mapper could do? Or am I just looking at it the wrong way and is this incorrect use of Hibernate?
Any feedback would be much appreciated!
Erwin