0

Does JPQL update query increment @Version field to support optimistic locking? I suppose no, but then, it means JPQL update would always pass, i.e. it would update entity no matter whether there is an active transaction trying to update the given entity. In this situation, the active transaction would pass since the @Version field wasn't changed, leading to the lost update. Am I right?

How would you avoid lost updates in this situation? Should I use pessimistic locking?

Mike
  • 812
  • 9
  • 25
  • You may refer https://stackoverflow.com/questions/2572566/java-jpa-version-annotation – Vivek Jan 17 '20 at 06:08
  • Does this answer your question? [Java - JPA - @Version annotation](https://stackoverflow.com/questions/2572566/java-jpa-version-annotation) – Dherik Jan 22 '20 at 23:00
  • @Dherik Accepted answer doesn't but the last comment does. I am going to add it as the answer to this question. – Mike Jan 28 '20 at 16:08

1 Answers1

0

JPA manages the version under the hood for you, however it doesn't do so when you update your record via JPAUpdateClause, in such cases you need to manually add the version increment to the query.

Mike
  • 812
  • 9
  • 25