Java Persistence API (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.
@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 is as follows: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 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.
@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;
}