Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version: 3.1.3
I am using optimistic locking with a Version column (Integer) in my hibernate-enabled application.
I am trying to apply some logic for the case the user changed a list of objects (in a GUI table, for example), and tries to save them all. In case one of the objects has already changed in the database, I want to catch the exception, find the object that changed in the db, and try saving all the other objects again. I tried doing that by catching the StaleObjectState exception, and then doing a session.evict(obj) on the object (I got it from the exception's members). It didn't work - after evicting I tried querying again for another object on the same table, which should cause a session flush, and the flush caused the update command to be sent again, and the StaleObjectState was raised again for me.
My test looks something like that:
1. read object1
2. read object2
3. change data in object1
4. change data in object2
5. stop in debug (I know it's crude, but I was just playing around to see how to get it to work before I started anything automatic), and change object1 version in the database directly.
6. try to read object3 <-- this causes the first exception, which is understandable.
7. find object1 form the session, using e.getPersistentClassName() and e.getIdentifier.
8. evict object1 from the session
9. read object3 again <-- this should have caused the update for object2 to be performed, but it causes the object1 update to be performed again, and the exception is raised again
what am I doing wrong? how can I make sure the changes are saved to the database, for all the objects that were not changed by some other user before, without opening a different transaction for each object?
Thanks for your input.
Noam Gal.
Problems with Session and transaction handling?
Read this:
http://hibernate.org/42.html