tags:

views:

939

answers:

3

I am using hibernate, spring, struts framework for my application.

In my application, each of the table has one field called as Version for tracking updation of any records.

Whenever i am updating existing record of my Country table which has version 0, it works fine & update the record update the version field to 1.

But whenever i am trying to update that version 1 record, it gives me error as follows:

org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException: Object of class [com.sufalam.business.marketing.model.bean.Country] with identifier [3]: optimistic locking failed; nested exception is org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.sufalam.business.marketing.model.bean.Country#3]

Is there any way to resolve it ?

A: 

"Row was updated or deleted by another transaction". Last time i received this I was doing things spread across more than one org.hibernate.Session object.

Schildmeijer
A: 

I don't remember exactly but are you supposed to hang on to the instance you saved? At least when usng JPA there is a return value from the merge method.

Konstantin
+1  A: 

The version column of Hibernate allows you to implement optimistic concurrency control.

Every time an object should be updated Hibernate checks if the version value stored in the database is the same as the version value in the object. If the two are different the StaleObjectStateException is thrown, meaning someone else has updated the object meanwhile the current session loaded, edited and stored it.

You have to make sure that the version value in your object is set to the correct value. Sometimes if you detach objects from the session and reattach them (merge) the value version column is not set correct (eg in web applications when values are retrieved from forms)

Thomas Einwaller