OptimisticLock Trying update the same object again

484 Views Asked by At

I'm getting the below exception:

org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [

It happens when I try to update an object using a Form again. So in the first time it works well but when I try to update the same object again i got that exception.

Could it be flush in the session ?

Here is my Entity
@PersistenceContext(unitName = "JPAService", type = PersistenceContextType.EXTENDED) private EntityManager nasc;

Here is my service: @Stateless @TransactionAttribute(TransactionAttributeType.REQUIRED) @TransactionManagement(TransactionManagementType.CONTAINER)

Thank you!

1

There are 1 best solutions below

0
On

Optimistic locking works the following way: you have a version field in your entity (annotated with @Version). You load this entity, and (for example), the version field has the value 33. You then save the entity. Hibernate checks that the version value in the entity (33) matches with the one in the table. If not, it throws this exception. If they match, it increments the versions, in the entity and in the database.

So, if you save the entity once again, but still gets the values from the form which still contains the old version value (33), you'll get this exception. Make sure to refresh the form object with the up-to-date values of the entity, including the version field.

It's impossible to give more details without any code, but this should enable you to debug and see where the bug is.