+

Search Tips   |   Advanced Search

Database generated version ID with JPA

Java Persistence API (JPA) for WAS extends OpenJPA to work with database generated version IDs for detecting changes in a given row.

Generation is supported for all WAS supported databases using two version strategies...

The Entity class is defined with the new Version Strategy annotation. The Entity has a surrogate version column. For example,

The create table statement...

During any updates to Item, insert or update, the VersionColumn value is updated in the database. After the update, the value for VersionColumn is retrieved from the database and updated in the in-memory object. Thereby the objects in the data cache reflect the correct version value. Here the Entity is using the @VersionColumn, which produces a Surrogate Version ID rather than defining an explicit field in the entity.

The Entity could also use @Version annotation to define an explicit version field. The explicit version field could be of type Long or Timestamp corresponding to the @VersionStrategy. During any updates to Item, insert or update, the Version value is updated in the database. After the update, the value for Version is retrieved from the database and updated in the in memory object. Thereby the objects in the data cache reflect the correct version value.

This is an example where the Entity has a version field defined, and the type Timestamp matches the RowChangeTimestampStrategy in the @VersionStrategy. If the version field type is using type long, then the RowChangeVersionStrategy should be annotated to match:

For DB2 V9.5, the generated database column must be of type timestamp. The RowChangeTimestampStrategy and the RowChangeVersionStrategy are both supported. Microsoft SQL Server only supports a non-timestamp generated version ID that goes with the RowChangeVersionStrategy. To use the RowChangeTimestampStrategy, use a trigger on a timestamp field in the database. For other databases, we can use triggers to simulate database version generation and use either strategy.