Skip to content

Commit

Permalink
more info about @Version in the doc
Browse files Browse the repository at this point in the history
  • Loading branch information
gavinking committed Dec 22, 2024
1 parent d3a93dd commit 436284a
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions documentation/src/main/asciidoc/introduction/Entities.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -432,16 +432,35 @@ Book book = session.find(Book.class, new BookId(isbn, printing));
[[version-attributes]]
=== Version attributes

An entity may have an attribute which is used by Hibernate for optimistic lock checking.
A version attribute is usually of type `Integer`, `Short`, `Long`, `LocalDateTime`, `OffsetDateTime`, `ZonedDateTime`, or `Instant`.
An entity may have an attribute which is used by Hibernate for <<optimistic-and-pessimistic-locking,optimistic lock verification>>.
A _version attribute_ is usually of type `Integer`, `Short`, `Long`, `LocalDateTime`, `OffsetDateTime`, `ZonedDateTime`, or `Instant`.

[source,java]
----
@Version
int version;
----

[source,java]
----
@Version
LocalDateTime lastUpdated;
----

Version attributes are automatically assigned by Hibernate when an entity is made persistent, and automatically incremented or updated each time the entity is updated.
A version attribute is automatically assigned by Hibernate when an entity is made persistent, and automatically incremented or updated each time the entity is updated.

If the version attribute is numeric, then an entity is, by default, assigned the version number `0` when it's first made persistent.
It's easy to specify that the initial version should be assigned the number `1` instead:

[source,java]
----
@Version
int version = 1; // the initial version number
----

Optimistic locks are verified by checking versions.
A version check is included in the `where` clause of every SQL `update` or `delete` statement for a versioned entity.
If a version check fails--that is, if no rows are updated--Hibernate throws an `OptimisticLockException` indicating that the current session is working with stale data--that is, that the entity was updated in some other unit of work.

[TIP]
// .Optimistic locking in Hibernate
Expand Down

0 comments on commit 436284a

Please sign in to comment.