The @Version annotation marks an entity field that JPA uses for optimistic locking. Every successful update increments the version, and the update statement only succeeds if the version in the database still matches the one the entity was loaded with. If another transaction changed the row first, the update affects zero rows and JPA throws an optimistic lock exception instead of silently overwriting the other change.
This prevents the lost update problem without holding database locks while users think or edit. The examples use Jakarta Persistence 3 (jakarta.persistence) with Hibernate 6, as in Spring Boot 3.
Two users open the same product. Both read price 100. User A changes it to 120 and saves. User B changes the stock count and saves the whole entity, including the stale price of 100. Without versioning, B's save overwrites A's change and nobody notices. Optimistic locking detects the stale read and rejects B's write.
The JPA specification allows int, Integer, short, Short, long, Long and java.sql.Timestamp for the version attribute. Hibernate supports additional temporal types, but a numeric counter is the safest choice because timestamps can collide at low precision. An entity may have only one @Version attribute, and you should never set it yourself.
When you change a managed Product and the transaction flushes, Hibernate issues an update guarded by the version:
If the statement updates one row, the version is now incremented. If it updates zero rows, someone else got there first and Hibernate throws jakarta.persistence.OptimisticLockException. The version only changes when the entity is actually dirty; reading an entity does not bump it.
The second commit throws OptimisticLockException (often wrapped in a RollbackException), and the transaction is rolled back.
In Spring, the exception is translated to ObjectOptimisticLockingFailureException, a subclass of OptimisticLockingFailureException. Decide what the caller should do: retry the whole unit of work, or tell the user to reload.
For automatic retries of short operations, re-run the entire transaction method (for example with Spring Retry), reloading the entity each time. Never retry with the same stale object.
update Product set ...) bypass the version unless you use Hibernate's update versioned form or increment the column yourself.Long lets Spring Data tell a new entity (null version) from an existing one; a primitive long defaults to 0, which can confuse that check.LockModeType.OPTIMISTIC_FORCE_INCREMENT bumps the version even if the entity did not change, which is useful to protect an aggregate root when a child changes.@Version the same as a database lock?No. It is optimistic: no row is locked while you work. Conflicts are detected at update time. Pessimistic locking (LockModeType.PESSIMISTIC_WRITE) locks the row instead.
Use Long or Integer. Timestamps work but can miss conflicts that happen within the same clock tick.
You should not. The persistence provider manages it, and changing it breaks conflict detection.
@Version work with Spring Data JPA save?Yes. save merges or persists the entity, and the guarded update applies on flush. A stale detached entity passed to save fails with an optimistic locking exception.
@Version adds a managed counter to an entity so that concurrent updates are detected rather than silently overwritten. Use a Long field, let JPA maintain it, catch OptimisticLockingFailureException and either retry or return 409 Conflict. It is cheap, scales well, and suits workloads where conflicts are rare.