In JPA, @Version marks the entity attribute that stores a version number or timestamp used for optimistic locking. The persistence provider increments it on every update and includes it in the WHERE clause, so an update based on stale data affects no rows and raises an optimistic lock exception.
You add one version field to an entity, and the provider does the rest. The sections below cover the mapping, the lifecycle of the value, the Spring Data behaviour, and the mistakes that make the annotation useless.
@Version belongs to jakarta.persistence (it was javax.persistence before Jakarta EE 9 and Spring Boot 3). It identifies a single persistent attribute as the version of the entity:
The database gets a version column. A new entity starts at 0, and each successful update increases it by one. The column holds only a counter, so it has no business meaning.
The lifecycle of the value looks like this:
Account; the provider sets version = 0.version = 0.version = 1.UPDATE account SET ..., version = 1 WHERE id = ? AND version = 0.OptimisticLockException and rolls back.Only entity state changes trigger an increment. If a flush finds the entity unchanged, the version stays the same.
The specification allows int, Integer, short, Short, long, Long and java.sql.Timestamp. Newer JPA releases extend the temporal choices. Prefer long or Long:
| Type | Advantage | Drawback |
|---|---|---|
Long | Simple, monotonic, small | None in practice |
Integer, Short | Smaller column | Can overflow on frequently updated rows |
Timestamp | Shows when the row last changed | Clock and precision issues allow collisions |
Put exactly one @Version attribute on an entity. In an inheritance hierarchy, declare it on the root entity or a @MappedSuperclass so that all subclasses share it.
Spring Data repositories need no extra code. A failed update surfaces as ObjectOptimisticLockingFailureException:
A nullable wrapper Long also helps Spring Data decide whether an entity is new: when the version attribute is a wrapper type and null, save() treats the entity as new and calls persist.
A conflict can only be detected across requests if the client sends back the version it read. Include version in your response DTO and compare it on update, or expose it as an ETag and require If-Match:
If you skip this comparison and copy DTO fields onto a freshly loaded entity, the provider checks the version of the loaded entity, not the client copy, and the stale edit goes through.
update versioned in HQL handles this.LockModeType.OPTIMISTIC_FORCE_INCREMENT or version the child too.@Version on a field in an embeddable or on a collection.It is part of the JPA specification, so it works with any compliant provider. Hibernate is the most common one and implements it as described here.
Yes. Add a non-null numeric column, with a default of 0 for existing rows, before deploying the mapped field.
Yes. It is an ordinary attribute, so select a.version from Account a works.
No. It adds conflict detection on top of transactions. You still need @Transactional boundaries to keep changes atomic.
@Version gives an entity optimistic locking with one field. The provider increments it on each update and rejects updates based on an old version. Use a long, send the version to clients, translate the exception into an HTTP 409 or a retry, and remember that bulk updates and child collections need extra care.