Java Reference
In-Depth Information
This example first constructs an instance of the
IntIdentity
class, which is a
built-in
JDO
single field identity class, and then calls
getObjectById()
, which
returns
PendingOrder
.
An important benefit of application identity is that the application can easily
access an object's persistent identity because it is stored in the object. This benefit
is especially important in web applications, which store object identifiers between
requests in the
HttpSession
or the browser by embedding them in cookies,
URL
s, or hidden form fields. For example, the Food to Go application stores the
identity of a
PendingOrder
in the
HttpSession
and embeds restaurant
ID
s in
URL
s on the page that displays the list of available restaurants. Because the object
identifier is stored in the object, the presentation tier can get the object identifi-
ers by calling a getter on the detached object returned by the business tier.
Another benefit of application identity is that the object's identity is almost
always a simple value such as an integer or a short string. This makes it straightfor-
ward to embed the object identifier in a
URL
or hidden field. In comparison,
when using datastore identity the string form of an object identifier is too long to
embed in a
URL
.
Application identity is also useful in applications that use a mixture of
JDO
and
JDBC/
i
BATIS
code. Because the
JDO
identity is the database primary key, you can
write
JDO
and
JDBC/
i
BATIS
code that exchanges primary keys. For example, you can
write
JDBC/
i
BATIS
code that executes a query and gets back some primary keys and
then uses
JDO
API
s to retrieve objects. Conversely, you can pass the persistent iden-
tity of a
JDO
object to
JDBC/
i
BATIS
code, which then executes a stored procedure.
The main drawback of using application identity is that because a primary key
usually has no meaning in the domain model, you must add a primary key field to
each class that uses application identity. Typically, however, the benefits of using
application identity outweigh the drawback of having to make some minor
changes to the domain model. If an application needs to access the persistent
identity of an object in its class, you should use application identity. On the other
hand, if you cannot add a primary key field to a class because you do not have
access to its source code, then you must use datastore identity.
Datastore identity
The other
JDO
identity mechanism is datastore identity, in which an object's per-
sistent identity is managed entirely by the
JDO
implementation. The
O/R
mapping
for a class specifies the primary key column(s) but does not map them to any of the
class's fields. Instead, the persistent identity of any in-memory
JDO
objects is main-
tained by
PersistenceManager
. The
JDO
implementation generates a persistent
Search WWH ::
Custom Search