The JDO identity guarantees that there is a single JDO instance associated to a persistence manager that represents a particular data store object. The SAP JDO implementation supports application identity only. This means that the identity of a JDO instance is determined by the values of a subset of the persistent fields (the primary key fields) in the persistence capable class. JDO requires the identity of a persistence capable class to be represented by instances of a special object identity class.
For the Employee and the Department persistence capable classes, you have to create object identity classes for the empId and depId fields respectively. These are the primary key fields of the classes that define the identity of their instances.
You must have defined the persistence capable classes Employee and Department.
To create object identity classes for Employee and Department classes, you have to follow the JDO requirements and recommendations:
· For a given persistence capable class, the corresponding object identity class is most conveniently defined as a public static inner class Id of the persistence capable classes.
· For each primary key field of the persistence capable class, the object identity class has a corresponding public instance field with the same name and type.
· The object identity class has a no-arguments (no-args) constructor similarly to the persistence capable classes.
· The object identity class also has a string constructor. It returns an instance that is equal to an instance returned as a string by the toString() method.
· The object identity class has to define hashCode and equals appropriately.
For the Employee class, extend the source code and add the inner object identity class, which is implemented as follows:
static public class Id {
// public field corresponding to the primary key of the PC class public int empId;
static { // establish the relation: Employee$Id class // is the identity class for the PC class Employee. SAPJDOHelper.registerPCClass(Employee.class); }
public Id() { // required: a no-args constructor }
public Id(int empId) { this.empId = empId; }
public Id(String string) { // required: a string constructor // defined as the counterpart of toString() empId = Integer.parseInt(string); }
public int hashCode() { // required: implement hashCode() return empId; }
public String toString() { // required: toString() defined // as the counterpart of the string constructor return Integer.toString(empId); }
public boolean equals(Object that) { // required: define equals() if (that == null || !(that instanceof Id)) return false; else return empId == ((Id) that).empId; } } |
For the Department class, the object identity class is coded as follows:
static public class Id {
// public field corresponding to the primary key of the PC class public int depId;
static { // establish the relation: Department$Id class // is the identity class for the PC class Department. SAPJDOHelper.registerPCClass(Department.class); }
public Id() { // required: a no-args constructor }
public Id(String string) { // required: a string constructor // defined as the counterpart of toString() depId = Integer.parseInt(string); }
public Id(int depId) { this.depId = depId; }
public int hashCode() { // required: implement hashCode() return depId; }
public String toString() { // required: toString() defined // as the counterpart of the string constructor return Integer.toString(depId); }
public boolean equals(Object that) { // required: define equals() if (that == null || !(that instanceof Id)) return false; else return depId == ((Id) that).depId; } } |
In order to compile the Employee and the Department classes once you have added the inner ID classes, you must add the following import declaration:
import com.sap.jdo.SAPJDOHelper; |
You can now define the XML metadata for the JDO.