Annotation Interface Version


@Target({METHOD,FIELD}) @Retention(RUNTIME) public @interface Version
Declares the version field or property of an entity class, which is used to detect optimistic locking conflicts, ensuring the integrity of optimistic transactions. The version field or property holds a version number or timestamp identifying the revision of the entity data held by an entity class instance.

An optimistic locking conflict occurs when verification of the version or timestamp fails during an attempt to update the entity, that is, if the version or timestamp held in the database changes between reading the state of an entity instance and attempting to update or delete the state of the instance.

The version attribute must be of one of the following basic types: int, Integer, short, Short, long, Long, java.sql.Timestamp, Instant, LocalDateTime.

This field declares a version number:

@Version
@Column(name = "REVISION")
protected int version;

This field declares a revision timestamp:

@Version
@Column(name = "LAST_UPDATED")
private Instant lastUpdated;

An entity class should have at most one Version field or property. The version field or property should be declared by the root entity class in an entity class hierarchy, or by one of its mapped superclasses.

The Version field or property should be mapped to the primary table of the entity.

The entity version must be updated by the persistence provider each time the state of an entity instance is written to the database. Furthermore, if the current persistence context contains a revision of the entity instance when the instance is written to the database, the persistence provider must verify that the revision held in the persistence context is identical to the revision held in the database by comparing the versions held in memory and in the database. If the versions do not match, the persistence provider must throw an OptimisticLockException.

The persistence provider is permitted to use the value of the identifier and version fields or properties of an entity instance to determine whether the instance is new or detached.

Since:
1.0
See Also: