In JPA, @Version designates the attribute that stores an entity's version number, enabling optimistic locking. The provider adds the version to every update's where clause and increments it on success, so a transaction working from stale data fails with an optimistic lock exception rather than overwriting a newer change.
This article focuses on using @Version in a Spring Data JPA application, including REST APIs where the version travels to the client and back. For the concurrency theory and a plain EntityManager walkthrough, the version field behaves identically; here the emphasis is on integration. Code targets Spring Boot 3 with jakarta.persistence.
@Version fits in a JPA entity@Version fits in a JPA entityThe column is created and maintained for you. A new entity starts at version 0, and each committed update increments it. If you use Flyway or Liquibase, add a non-null version column with a default of 0 to existing tables before enabling the annotation.
| Java type | Notes |
|---|---|
int, Integer, short, Short | Simple counters, the usual choice |
long, Long | Best for long-lived, frequently updated rows |
java.sql.Timestamp | Allowed by the specification, but precision can cause missed conflicts |
Hibernate accepts further temporal types, but a numeric counter is portable and predictable.
If two threads run deposit for the same account at once, one of them commits first. The other's update matches zero rows and Spring raises ObjectOptimisticLockingFailureException. Because the whole method is one transaction, nothing is half-applied.
A deposit is safe to retry because it reloads the current state each time. Wrap the transactional call, not the inside of it:
The retry loop must live outside the @Transactional proxy so each attempt gets a fresh transaction and a freshly loaded entity.
For edits that span several HTTP requests, the version must go to the client and come back with the update, otherwise the check only covers the short server-side transaction. A common pattern is to send it as an ETag and read it from If-Match.
On update, load the entity, compare the client's version with the stored one, and respond 412 Precondition Failed (or 409 Conflict) on mismatch. Do not copy a client-supplied version into the entity: compare it, then let JPA manage the field.
JPA lock modes build on the same column:
LockModeType.OPTIMISTIC re-checks the version at commit even for an entity you only read.LockModeType.OPTIMISTIC_FORCE_INCREMENT always increments the version, which helps protect an aggregate when only a child entity changed.In Spring Data, apply them with @Lock(LockModeType.OPTIMISTIC) on a repository method.
jakarta.persistence.OptimisticLockException. Under Spring it is translated to ObjectOptimisticLockingFailureException.
@Version on every entity?Only on entities that can be updated concurrently and where a lost update matters, such as balances, inventory and shared documents.
Nothing changed (the entity was not dirty), you used a bulk update query, or the field is not mapped with @Version.
Yes, and for multi-request edits you should, so the client can send it back for comparison.
@Version gives JPA entities optimistic locking with a single extra column. In Spring Data, keep updates inside a transaction, catch OptimisticLockingFailureException, retry short operations and return 409 or 412 for user edits. Pass the version to clients through an ETag when edits span several requests.