Chapter 10. Identity

Java defines two concepts that determine whether two instances are the same: identity and equality. Two instances have the same Java identity if and only if they occupy the same memory location within the Java Virtual Machine (JVM). Java identity is managed entirely by the JVM, whereas Java equality is determined by the class. Two distinct instances with different identities are equal if they represent the same value, based on the abstraction being modeled. For example, two distinct instances of Integer with separate Java identities may have the same integer-abstraction value; they are considered equal. Or, two distinct HashSet instances may contain the same elements and be considered equal, even though they may have a completely different organization of their internal data structures, as a result of the order in which elements were added and removed. If you are a Java developer, you likely understand the Java concepts of identity and equality already.

JDO has its own requirements for uniquely identifying a persistent instance. The same datastore instance can be in multiple transactions in the JVM at the same time, so the Java notion of identity cannot be used. The application doesn't necessarily implement equals( ), so it cannot be used.

Therefore, JDO defines its own identity abstraction to identify an instance uniquely in the datastore. This identity is used in the datastore to establish a reference to an instance. It is also used to determine if two in-memory instances represent the same object in the datastore. We refer to this new form of identity as JDO identity, when necessary, to distinguish it from Java identity. JDO identity is defined differently from both Java identity and Java equality.

The JDO implementation manages a cache of persistent instances for each PersistenceManager, such that each instance from the datastore is represented by a single instance in the cache of the PersistenceManager. This cache is not a specific region of memory; it simply consists of the set of all instances managed by the PersistenceManager. The JDO implementation allows an application to navigate through persistent references and collections of references accessed from the datastore by using simple Java references. The JDO identity of the persistent class determines the representation of these references in the datastore and how the implementation accesses an instance in the datastore when your application uses a reference.

If the JVM has multiple PersistenceManager instances, each has its own associated cache of persistent instances. Two or more of these PersistenceManager instances may have their own distinct copy of the same datastore instance. In this case, each copy of the datastore instance has a distinct Java identity, but they all have an identical JDO identity.