Java Reference
In-Depth Information
Unlike with the
java.io.Serializable
that makes a class serializable, you do not explicitly declare
your class as "
implements PersistenceCapable
". Look at the persistent class (
Yacht
) shown in
Listing 23-1
; simply declare the class as follows, without mentioning the
PersistenceCapable
interface:
public class Yacht {
… …
}
This is the beauty of transparent persistence. In most JDO implementations, you specify that the class
meant to be persistent in an XML MetaData file read by the JDO enhancer. The JDO enhancer modifies
the class's bytecode to ensure that it implements
PersistenceCapable
prior to loading the class into
the runtime environment. The JDO enhancer also adds code to implement the methods defined by
PersistenceCapable
.
As an example, the XML MetaData file for the persistent class
Yacht
is shown in
Listing 23-2
. The
document-type definition file,
jdo.dtd
, is provided by the vendor of the JDO implementation that you
use.
Listing 23-2: XML MetaData file for the persistent class Yacht
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jdo SYSTEM "jdo.dtd">
<jdo>
<package name="java_database.jdo">
<class name="Yacht" identity-type="datastore">
</class>
</package>
</jdo>
You tell the JDO enhancer that the class
java_database.jdo.Yacht
is to be persisted in a data
store. If you have a complex persistable object model with inheritance and other types of object
relationships, the XML MetaData file is, of course, more complex than what you see in
Listing 23-2
.
But
it is almost never so lengthy and so complicated as a CMP entity bean's deployment descriptor.
Since the JDO enhancer does all the work behind the scenes, mapping your persistent classes with the
persistent store, you do not have to know the details of the
PersistenceCapable
interface.
PersistenceManagerFactory Interface
The
javax.jdo.PersistenceManagerFactory
interface obtains
PersistenceManager
instances. All
PersistenceManager
instances obtained from the same
PersistenceManagerFactory
have the same default properties.
PersistenceManagerFactory
instances may be configured and serialized for later use. They may
be stored via the Java Naming and Directory interface (JNDI) and looked up and used later. Any
properties configured are saved and restored. Once the first
PersistenceManager
is obtained from
the
PersistenceManagerFactory
, the factory can no longer be configured.
The application acquires an instance of JDO
PersistentManager
by calling the
getPersistentManager
method of an instance of JDO
PersistenceManagerFactory
. The code
may looks like this:
…
InitialContext ctx = new InitialContext();