I would just like to go over the functioning of version control.
We use Hibernate 2.1.3 to MySQl 4.0.18 in a three tier (Struts/ Spring /Hibernate) system.
The object data is stored in ActionForms between http sessions.
So to save the changes in the ActionForm back to the database,
when the object has been modified by a previous session,
we go through the following steps in the service layer:
1. locate the hibernate object using the id from the ActionForm
2. copy the data from the ActionForm into the Hibernate model object
3. save the model object with changes
Here's the code:
Note we have version tag (type integer) on the modified field...
public Object saveUser(UserFormEx obj) throws Exception {
User oldUser = dao.getUser(obj.getId());
System.out.println("Old from database = "+oldUser.getModified());
System.out.println("In from form = "+obj.getModified());
copyToUser(obj,oldUser);
System.out.println("After copy = "+oldUser.getModified());
dao.saveUser(oldUser);
System.out.println("After Save = "+oldUser.getModified());
return convertUser(oldUser);
}
The output from this function is:
Old from database = 10
In from form = 9
After copy = 9
After Save = 11
I would have expected as the modified field of the form is not equal to current (oldUser) so a Stale Object Exception would be thrown.
Checking the SQL an update where id = ? and modified = ? is successful and any modified user data is saved.
The obvious answer being hibernate saves the old modified field and overwriting it in the copyToUser method does nothing...
The solution is for us is to do the check in the application, however I was wondering why we can't change the modified field forcing the stale exception?
|