Java Reference
In-Depth Information
primary-key="true" value-strategy="native" />
…
</class>
This example shows part of the
PendingOrder
class and an excerpt of its
XML
metadata. To use application identity with the
PendingOrder
class we must add
an
ID
field to store the primary key. It usually makes sense to also define a getter
so that the rest of the application can access the
ID
. This example configures the
JDO
identity of the
PendingOrder
class using the following attributes:
The
identity-type="application"
attribute specifies that you want to
use application identity.
■
The
primary-key="true"
attribute specifies that the
PendingOrder.id
field will store the primary key.
■
The
value-strategy="native"
attribute tells the
JDO
implementation to
pick the most suitable identifier generation strategy based on the underly-
ing database.
■
When the application calls
PersistenceManager.makePersistent()
to save a
newly created
PendingOrder
object, the
JDO
implementation will generate the
primary key using one of a variety of key generation mechanisms, including data-
base sequences and auto-increment columns, and store it in the
ID
field.
The application can also assign values to the primary key field(s) before calling
PersistenceManager.makePersistent()
. This can be useful when you're map-
ping a domain model to a legacy schema that uses a natural primary key instead of
a surrogate primary key. If necessary, either the application can implement its
own key-generation mechanism or it can call
JDO
to generate a primary key value.
A class that uses application identity must have an object
ID
class, which
defines fields corresponding to the names of the class's primary key fields. An
application loads an existing object with a particular primary key by passing an
instance of the object
ID
class that contains the primary key to a method, such as
getObjectById()
. If a class has a single primary key field, which is termed
single
field identity
, then the application uses one of the built-in single field identity pri-
mary key classes. However, if the class has multiple primary key fields, then the
application defines a custom object
ID
class.
Here is an example of how an application would retrieve a
PendingOrder
with
an
ID
of
555
when using application identity:
String idString = "555";
IntIdentity objectId = new IntIdentity(PendingOrder.class, idString);
PendingOrder p = (PendingOrder)pm.getObjectById(objectId);
Search WWH ::
Custom Search