4.1 Kinds of Classes and Instances
First, we must define some terms and provide some distinctions that
are essential for understanding JDO. The term
"object" often refers to either a
class or an instance of a class, which can be confusing sometimes.
Therefore, we will use the terms
"instance" and
"class" instead of
"object," because it will be
essential for you to understand which we are discussing.
4.1.1 Kinds of Classes
When using JDO, every class falls into one of the following two
categories:
- Persistent class
-
A persistent class can have its instances stored in the datastore. To
be persistent, a class must be specified in a metadata file and
enhanced. The JDO specification refers to these as
persistence-capable classes.
- Transient class
-
A transient class cannot have its instances stored in the datastore.
Transient classes are not listed in a metadata file.
Furthermore, classes can be distinguished by their use of the JDO API:
- JDO-aware class
-
A JDO-aware class makes direct use of the JDO API. For example, it
can perform a JDO query to retrieve instances from the datastore, or
make specific instances persistent.
- JDO-transparent class
-
A JDO-transparent class does not make direct use of the JDO API.
Whether a class is JDO-aware or JDO-transparent is unrelated to
whether it is persistent. For example, the persistent classes
Movie, Actor, and
Role that we introduced in Chapter 1 are JDO-transparent, because they never made
an explicit call to the JDO API. On the other hand, the
MediaManiaApp class is JDO-aware, because it uses
the JDO API directly: it creates a
PersistenceManager and uses it to execute
transactions. MediaManiaApp is not persistent.
4.1.2 Kinds of Instances
JDO supports several kinds of instances. The names we introduce in
this section are used throughout the book to refer to these different
kinds of instances. In particular, we use specific terminology to
differentiate a transient instance of a transient class from a
transient instance of a persistent class. All JDO implementations
support the first three kinds of instances listed here; the last two
are optional:
- Instance of a transient class
-
All instances of a transient class are transient. For the most part,
however, we focus on instances of persistent classes.
- Transient instance
-
A transient instance is an instance of a persistent class that is not
associated with the datastore. It is simply an instance you create in
your application that is never made persistent and is used
independent of the datastore.
- Persistent instance
-
A persistent instance is an instance of a persistent class whose
behavior is linked to a transactional datastore. Its fields are
watched by the JDO implementation and saved to or restored from the
datastore, as appropriate. The datastore manages the state of its
persistent fields and information identifying its class.
- Transient transactional instance
-
A transient transactional instance is transient and is not
represented in the datastore. But it is transactional, and its state
is rolled back if a transactional rollback occurs. For JDO to manage
a transient transactional instance, you need to enhance its class.
Transient transactional instances are covered in Chapter 13.
- Persistent nontransactional instance
-
A persistent-nontransactional instance is persistent, but it is not
managed as part of a transaction. Persistent nontransactional
instances are discussed in Chapter 14.
Table 4-1 illustrates these different kinds of
instances, based on their persistence and transactional behavior.
Table 4-1. Kinds of instances|
Transactional
| |
Transient transactional instance
|
Persistent instance
|
Nontransactional
|
Instance of a transient class
|
Transient instance
|
Persistent nontransactional instance
|
|