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 ...

Get Java Data Objects now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.