version

Optimistic Version.

Hibernate allows the user to make the changes and exceptions are thrown when the updated object is synced with the database at the end of the transaction.

Optimistic versioning is enabled as soon as a <version> or <timestamp> is added to the class mapping.

It can be turned off using optimistic-lock="false" on the particular attribute, collection. (inverse is ignored)

update object set property='dirty' where objId =1 and version =1

if the update returns no rows updates Hibernate throws a StateObjectStateException.

object version is changed for each of the properties unless marked as mentioned above.

Mapping

1. add <version> to the class mapping.

Whenever the object is updated the version is sent as

With Version

Without Version

object version is checked automatically within the transaction in the same session.

Mapping

optimistic-lock="all" needs to set on the class mapping.

SQL generated to update the object includes all the property value as shown below when

dirty-property01 is changed.

update object set property='dirty' where objId =1

and dirty-property01='dirty-01'

and non-dirty-property02='nondirty-02'

.

.

.

and non-dirty-propertyN='nondirty-N'

the effect can be achieved by setting

optimistic-lock="dirty" and dynamic-update="true"

here the update SQL is generated with only those properties which are dirty.

update object set property='dirty' where objId =1

and dirty-property01='dirty-01'