Hello
I found few posts on the subject but they didn't really help, so I imagined I could ask once more - testing your patience :-)
Here goes my case:
From time to time, StaleObjectStateException gets thrown, indicating another user of my system modified something "behind the scenes". Of course, being prepared for this - I catch the exception and inform user of problems that occured. Sometimes user does not want to hear about all this and simply wants to save his work in 'no matter what' manner. This should be relatively simple to implement - was my first thought - but I've been having really hard time with this scenario. I will present little real code, because Hibernate is underneath my own DAO layer - this would be no help to other users, but here goes description of what I do:
1. reload from session conflicting object
Code:
session.load(persistentClass, id, LockMode.NONE);
2. rollback "old" transaction and close the session
3. reattach conflicting object from 1 with new session and force update
3. is my pain - I tried everything that, I believed, would work - tried to call session.merge - nope - another StaleObjectStateException, tried to call session.update - the same, tried to lock with LockMode.UPGRADE - guess what happened :-(. I simply want to force Hibernate to execute another update with old data and new version number, plain old simple overwite. How can I do this? What really works is scenario when user, informed about concurrentm odification wants to leave his work behind - then combination of refresh and merge works, enabling him to continue his work on new data. What is the recommended pattern of dealing with this kind of situation? Thank you in advance for your answers.
PS. I am using Hibernate 3.2.6 GA