intermediateConcurrency — Optimistic & Pessimistic Locking, MVCC
How does @Version implement optimistic locking under the hood?
Hibernate includes the loaded version value in the generated UPDATE's WHERE clause and increments it on write. If another transaction already updated (and bumped the version of) the same row, the WHERE clause matches zero rows — Hibernate detects this via the affected-row count and throws OptimisticLockException.
Ready to master this question?
Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.
Sign in to generate a response