The basic process is:
1. Hibernate session A loads object A1 (with identifier 1234)
2. Hibernate session B loads object B1 (also with identifier 1234)
3. Session A modifies object A1 and saves it
4. B modifies object B1 and attempts to save it
5. Hibernate will detect the version number for the object has changed and generate an exception (StaleObjectStateException)
6. Catch this exception, close the hibernate session, return an error message to the user using ActionErrors etc
I also have a refresh button so that at any time the user can press refresh which reloads the object from hibernate so it is up to date. When I return my stale object error I also allow the user to either refresh (loses the users changes), cancel (do nothing, return to parent menu), or overwrite. Overwrite can attempt to overwrite the changes by reloading the new object, but use the posted field changes to update and then save the object.
You should check out versioning on the hibernate web site for more info on that.
David Erickson wrote:
Hi I am using Struts with Hibernate in a webapplication.. we are using forms etc. The problem I am currently trying to decide how to handle is thus:
Assume user 1 loads up an object in a form and is modifying it. Assume user 2 loads up the same object in a form and is also modifying it. User 1 submits the modified object. User 2 also submits the object, however his is out of date and I would like the webapp to tell him that and show the differences that exist.
What is the best way to use hibernate to deal with this? What I have attempted is using the built in timestamp feature in MySQL, I have a field in my object that is timestamp but does not insert or update, thus mysql controls its value. Then when the user submits the object I loaded another copy of that object from the DB and tried to compare their dates.. however this gave me an error saying:
net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: 1, of class: cmcflex.salesweb.model.prospect.Prospect
because of the object I loaded to compare.
What do do? Is there a better strategy for tackling this? I tried using the actual <timestamp> in the properties for my object, but then when I tried to submit the object the generated sql said "when id=? AND timestamp=?" meaning it would not update if the timestamp is different, and I'm unsure if I could even determine if it did or did not update.
Thanks in advance, David
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
-- Jason Lea
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]