Database generated version ID

JPA for WebSphere Application Server has extended OpenJPA to work with database generated version IDs. These generated version fields (timestamp or token) can be used to efficiently detect changes to a given row.

Trigger based version ID generation is supported for all databases that WebSphere Application Server supports. Support is based on two Version Strategies in Java Persistence API (JPA) for WebSphere Application Server.

Database generated version ID example

In this example, the Entity class is defined with the new Version Strategy annotation. The Entity has a surrogate version column.
@Entity(name="Item")
@VersionColumn(name="versionField")
@VersionStrategy("com.ibm.websphere.persistence.RowChangeTimestampStrategy")
public class Item implements Serializable
{	
	@Id
	private int id2;
	
	private String name;
	
	private double price;

   @OneToOne
   private Owner master;
}
The create table statement for this would be:
CREATE TABLE ITEM
(ID2  INT  NOT NULL,
 NAME  VARCHAR(50) ,
 PRICE   DOUBLE,
 OWNER_ID INT,
             VERSIONFIELD GENERATED ALWAYS FOR EACH ROW ON 
              UPDATE AS ROW CHANGE TIMESTAMP
									PRIMARYKEY(ID2));
During any updates to Item (insert or update) the VersionColumn value will be 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 will be updated in the database. After the update the value for Version would be retrieved from the database and updated in the in memory object. Thereby the objects in the data cache would reflect the right 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) :
@Entity(name="Item")
@VersionStrategy("com.ibm.websphere.persistence.RowChangeTimestampStrategy")
public class Item implements Serializable
	
{
	@Id
	private int id2;
	
	private String name;
	
	private double price;
   
   @Version
   private Timestamp versionField;

   @OneToOne
   private Owner master;
}

For Z/OS db2 v9 the generated database column must be of type timestamp but we support both the RowChangeTimestampStrategy and the RowChangeVersionStrategy. MS SqlServer only supports a non timestamp generated version ID that goes with the RowChangeVersionStrategy . To use the RowChangeTimestampStrategy, you must use a trigger on a timestamp field in the database. For other databases you can use triggers to simulate database version generation and use either strategy.




Related tasks
Associating persistence units and data sources
Concept topic Concept topic    

Terms and conditions for information centers | Feedback

Last updatedLast updated: Aug 31, 2013 2:56:59 AM CDT
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=pix&product=was-nd-dist&topic=cejb_genversionID
File name: cejb_genversionID.html