Java Persistence API (JPA) Extensions Reference for EclipseLink, Release 2.4
  Go To Table Of Contents


Use @OptimisticLocking to specify the type of optimistic locking EclipseLink should use when updating or deleting entities.

Annotation Elements

Table 2-45 describes this annotation's elements.

Table 2-45 @OptimisticLocking Annotation Elements

Annotation Element Description Default


(Optional) Specify where the optimistic locking policy should cascade lock. When changing private owned and delete orphan object, EclipseLink will update the version.

Currently only supported with VERSION_COLUMN locking.



(Optional) Specify a list of columns that will be optimistically locked.

This element is required when type=SELECTED_COLUMNS.


(Optional) The type of optimistic locking policy to use:

  • ALL_COLUMNS – EclipseLink compares every field in the table with the WHERE clause, when performing and update or delete operation.

  • CHANGED_COLUMNS – EclipseLink compares only the changed fields in the WHERE clause when performing an update.

  • SELECTED_COLUMNS – EclipseLink compares the selected field in the WHERE clause when performing and update or delete operation on the SelectedColumns.

  • VERSION_COLUMN – EclipseLink compares a single version number in the WHERE clause when performing an update.



You can specify @OptimisticLocking on an Entity or MappedSuperclass.


Example 2-83 shows how to use the @OptimisticLocking annotation for all columns

Example 2-83 Using @OptimisticLocking Annotation

@Table(name = "EMPLOYEES")
  public class Employee implements Serializable {

Example 2-83 shows how to use the <optimistic-locking> element in the eclipselink-orm.xml file for a single column.

Example 2-84 Using <optimistic-locking> XML

<entity name="Employee" class="my.Employee" access="PROPERTY" change-tracking="DEFERRED">
    <optimistic-locking type="SELECTED_COLUMNS" cascade="false">
      <selected-column name="id"/>
      <selected-column name="firstName"/>

See Also

For more information, see: