Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Locking

Locking

You have the choice between optimistic and pessimistic locking. We recommend using EclipseLink optimistic locking.

By default, EclipseLink persistence provider assumes that the application is responsible for data consistency.

Use the @Version annotation to enable the JPA-managed optimistic locking by specifying the version field or property of an entity class that serves as its optimistic lock value (recommended).

When choosing a version field or property, ensure that the following is true:

  • there is only one version field or property per entity;
  • you choose a property or field persisted to the primary table (see Section 9.1.1 "Table Annotation" of the JPA Specification);
  • your application does not modify the version property or field.

Elug note icon.png

Note: The field or property type must either be a numeric type (such as Number, long, int, BigDecimal, and so on), or a java.sql.Timestamp. We recommend using a numeric type.


The @Version annotation does not have attributes.

The Usage of @Version Annotation example shows how to use this annotation to specify property getVersionNum as the optimistic lock value. In this example, the column name for this property is set to OPTLOCK (see Section 9.1.5 "Column Annotation" of the JPA Specification) instead of the default column name for the property.

Usage of @Version Annotation

 @Entity
 public class Employee implements Serializable {
     ...
     @Version
     @Column(name="OPTLOCK")
     protected int getVersionNum() {
         return versionNum;
     }
     ...
 }

The @Version annotation supports the use of EclipseLink converters (see Using EclipseLink JPA Converters).

For more information, see the following:

For more information on the EclipseLink artifacts configured by these JPA metadata, refer to Descriptors and Locking.


Eclipselink-logo.gif
Version: DRAFT
Other versions...

Back to the top