org.springframework.orm.ObjectOptimisticLockingFailureException: Object of class [com.payumoney.paymentUtil.model.EntityTDR] with identifier [389853]: optimistic locking failed; nested exception is org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [com.payumoney.paymentUtil.model.EntityTDR#389853]
Solution :
In the Entity or Model if we are using version column and if we are not mentioned below annotation then we will get this issue
Solution :
In the Entity or Model if we are using version column and if we are not mentioned below annotation then we will get this issue
@Generated(GenerationTime.ALWAYS)
Key points:
- The relationship between the parent and child is bi-directional one-to-many.
- We use optimistic locking with the version column being a timestamp created by MySQL either during insert or during update. On the version field we specify Generated(GenerationTime.ALWAYS) to ensure that the version details are obtained from the database automatically (avoid the time precision issue between Java and MySQL)
- During saving a new entity (id = 0), I can see the logs that the entity is being inserted into the database, I can also see the child entities being inserted in the database (via the Hibernate logs). During this process, I can also see the a select is done to get the version details from the database.
- Soon after the entities are inserted and the session is being flushed, there is a dirty checking is done on the collection and I see a message in the log that the collection is (). Straight after this, I see an update statement on the parent entity’s table and this is where the problem occurs as the version value used in the update statement is different to what is in the database, the exception is thrown.
No comments:
Post a Comment