Java Reference
In-Depth Information
identity using a variety of mechanisms, including database sequences and auto-
increment columns, but does not store the identity in the object.
Here is an example of using datastore identity with the
PendingOrder
class:
<class name="PendingOrder" identity-type="datastore" >
<datastore-identity strategy="native"
column="PENDING_ORDER_ID"/>
…
</class>
Because we are using datastore identity, we do not add a primary key field to the
PendingOrder
class. This example configures the
JDO
identity of the
Pending-
Order
class as follows:
The
identity-type="datastore"
attribute specifies that the
Pending-
Order
class uses datastore identity. Note, however, that because
datastore
is
the default value of this attribute (if no field is flagged with
primary-
key="true"
within this class mapping), this can be omitted.
■
The
<datastore-identity>
element configures the datastore identity. The
column="PENDING_ORDER_ID"
attribute specifies that
PENDING_ORDER_ID
is the primary key column. The
strategy="native"
attribute tells the
JDO
implementation to use the most appropriate primary key generation mech-
anism for the underlying database.
■
As you can see, the primary key column is not mapped to a field in the object. An
application must call
JDOHelper.getObjectId(object)
to get the identity of an
object. This method returns an instance of an implementation-specific object
ID
class. The application can convert an object
ID
to a string by calling
toString()
.
It could then, for example, store that string
ID
in the
HttpSession
or store it in
the browser in a cookie, or hidden field, or as a
URL
parameter.
Later on, the application can convert a string obtained in this way back to an
object
ID
by calling
PersistenceManager.newObjectIdInstance()
. Here is an
example of how an application would retrieve a
PendingOrder
with a particular
ID
when using datastore identity:
HttpServletRequest request = …;
String idString = request.getParameter("pendingOrderId");
Object objectId = pm.newObjectIdInstance(PendingOrder.class,
idString);
PendingOrder p = (PendingOrder)pm.getObjectById(objectId);
The
newObjectIdInstance()
method returns an instance of an object
ID
class,
which is then passed to
getObjectId()
. Note that, unlike this code snippet, a
Search WWH ::
Custom Search