With application identity you are taking control of the specification of id's to JPOX. Application identity requires a primary key class, and each persistent capable class may define a different class for its primary key, and different persistent capable classes can use the same primary key class, as appropriate. To specify that a class is to use application identity, you add the following to the MetaData for the class.
<class name="MyClass" identity-type="application" objectid-class="MyIdClass">
<field name="myPrimaryKeyField" primary-key="true">
...
</class>
That is, specifying the "identity-type" and "objectid-class". The "objectid-class" is the class defining the identity for this class. You then need to define the primary key class.
If you wish to use application identity you must define a Primary Key class. You can't use classes like java.lang.String, or java.lang.Long directly. You must follow these rules when defining your primary key class.
Here's an example of a simple (single field) primary key class
import java.io.Serializable;
public class SimpleIdKey implements Serializable
{
public String myPrimaryKeyField;
/**
* Default constructor.
*/
public SimpleIdKey () {}
/**
* String constructor.
*/
public SimpleIdKey (String value)
{
StringTokenizer token = new StringTokenizer (value, "::");
//className
token.nextToken ();
//myPrimaryKeyField
myPrimaryKeyField = token.nextToken ();
}
/**
* Implementation of equals method.
*/
public boolean equals (Object object)
{
if (this == object)
{
return true;
}
if (!(object instanceof SimpleIdKey))
{
return false;
}
SimpleIdKey other=(SimpleIdKey)object;
return this.myPrimaryKeyField.equals(other.myPrimaryKeyField);
}
/**
* Implementation of hashCode method that supports the
* equals-hashCode contract.
*/
public int hashCode ()
{
return this.myPrimaryKeyField.hashCode();
}
public String toString ()
{
return this.getClass().getName() + "::" + this.myPrimaryKeyField;
}
}
Here's an example of a composite (multiple field) primary key class
public class ComposedIdKey implements Serializable
{
public String field1;
public String field2;
/**
* Default constructor.
*/
public ComposedIdKey ()
{
}
/**
* String constructor.
*/
public ComposedIdKey(String value)
{
StringTokenizer token = new StringTokenizer (value, "::");
//className
token.nextToken ();
//field1
this.field1 = token.nextToken ();
//field2
this.field2 = token.nextToken ();
}
/**
* Implementation of equals method.
*/
public boolean equals(Object obj)
{
if (obj == this)
{
return true;
}
if (!(obj instanceof ComposedIdKey))
{
return false;
}
ComposedIdKey c=(ComposedIdKey)obj;
return field1.equals(c.field1) && field2.equals(c.field2);
}
/**
* Implementation of hashCode method that supports the
* equals-hashCode contract.
*/
public int hashCode ()
{
return this.field1.hashCode() ^ this.field2.hashCode();
}
/**
* Implementation of toString that outputs this object id's PK values.
*/
public String toString ()
{
return this.getClass().getName() + "::" + this.field1 + "::" + this.field2;
}
}
|