nested exception is org.hibernate.StaleObjectStateException?
I am having problem understanding why I get this exception.
I have a parent object with a one-to-one mapping to a child... I then changes the child and call update to save the parent object. This saves the changes in the child object to the database and then I call get(...) to load the parent and use these new data to show my webpage. If I then tries to call update on the parent again (with no changes to parent or child), I get this exception, but why? The object contains all the new updated data from the database and no one else is updating the database.
Am I missing something, shouldn't this work?Regards,
BTJ
It appears that your are trying to save an old object while Hibernate has already done an update. There are different causes for this but it's best if you can post the relevant code snippet. You might find the solution for yourself if you turn on logging and look at what HB is doing.
What do you mean by quot;save an old objectquot;? What I am doing is that in the first session, I load the object using:
getHibernateTemplate().get(...)
and moves the data to DTO object...When I save the object in the second session, I create a new domain model object and populates with data from the DTO object (which is saved in from session scope) and updates this object using:
getHibernateTemplate().update(obj);
getHibernateTemplate().flush();And then, when I do the same thing in my third session (as in my second session), I get this exception..
I have also tried to use the same get(...) method after save, to make sure that the version fields are updated with the newest data (and also then updating my DTO), but still the same error..
Looking at the HB log doesn't really tell me anything except that this exception happens when it tries to update the same object (child) that was updated first (which it really shouldn't do, because that object hasn't changed...)BTJ
btw, the only field HB is trying to update on the object that fails, is the version field, but this should not be updated without a change in that object, right?BTJ
Hibernate manages the object by itself - you can change a field from A to B and then back to A but even if it's the same Hibernate will update it and save it. You can see what happens to your objects through the logs.
But my way of thinking is correct, or?
BTJ
Well, I finally solved my problem... The problem was that I was using timestamp as my version field and the timestamp I have in the database is without timezone and I guess HB uses timezone in it's timestamp because after switching to a long version field instead, I no longer get this exception....
BTJ |