Hi,
I have folowing problem. I have client A and B. Both fetches same row. A changes something and save it. B change something and save it. It gets exception about stale data. Since here OK.
Now I want to show user his data and data in DB. User then can choose to overwrite with his data or to leave the current state in DB. I want ask if I am doing it in right way.
Here is he part when overwriting the data with user's B (overwriting changes of user A).:
private overwritePerson(PersonData personData)
{
sess = HibernateUtil.currentSession(); //return the same session as used for reading, only reconnect it.
Transaction tx = sess.beginTransaction();
Person person = loadObjectPersonByPK(new Long(personData.getId()); //loads the person already associated with the session before - so no direct access to DB only to local cache
sess.refresh(person); //refreshing with current data from DB - so getting the data that user A changes
per.setData(personData); //set datas from user B
sess.saveOrUpdate(per); //save them
sess.flush();
tx.commit();
sess.close();
}
Is that refresh ok ? Or is there other way to tell hibernate to save some object and bypass the version check ? Or is is better to load second person using new session ?
Thanks for tips.
Here is mapping file:
<hibernate-mapping>
<class name="hibernate.test.Person"
table="person"
dynamic-update="true"
optimistic-lock="all"
>
<id name="id" type="long" unsaved-value="null" >
<generator class="sequence">
<param name="sequence">person_sequence</param>
</generator>
</id>
<version name="version"
type="long"/>
<property name="name"/>
<property name="surname"/>
</class>
</hibernate-mapping>
|