JDO allows objects of classes to be versioned. The version is typically used as a way of detecting if the object has been updated by another thread or PersistenceManager since retrieval using the current PersistenceManager - for use by Optimistic Transactions. JDO defines several "strategies" for generating the version of an object. The strategy has the following possible values
JDO2s mechanism for versioning of objects in RDBMS datastores is via a surrogate column in the table of the class. In the MetaData you specify the details of the surrogate column and the strategy to be used. For example
<package name="mydomain"> <class name="User" table="USER"> <version strategy="version-number" column="VERSION"/> <field name="name" column="NAME"/> ... </class> </package>
alternatively using annotations
@PersistenceCapable @Version(strategy=VersionStrategy.VERSION_NUMBER, column="VERSION") public class MyClass { ... }
The specification above will create a table with an additional column called "VERSION" that will store the version of the object.
DataNucleus provides a valuable extension to JDO whereby you can have a field of your class store the version of the object. This equates to JPA's versioning process whereby you have to have a field present. To do this lets take a class
public class User { String name; ... long myVersion; }
and we want to store the version of the object in the field "myVersion". So we specify the metadata as follows
<package name="mydomain"> <class name="User" table="USER"> <version strategy="version-number"> <extension vendor-name="datanucleus" key="field-name" value="myVersion"/> </version> <field name="name" column="NAME"/> ... <field name="myVersion" column="VERSION"/> </class> </package>
alternatively using annotations
@PersistenceCapable @Version(strategy=VersionStrategy.VERSION_NUMBER, column="VERSION", extensions={@Extension(vendorName="datanucleus", key="field-name", value="myVersion")}) public class MyClass { protected long myVersion; ... }
and so now objects of our class will have access to the version via the "myVersion" field.