Every entity object has a unique identity within its home. If two entity objects have the same home and the same primary key, they are considered identical. The Enterprise JavaBeans architecture allows a primary key class to be any class that is a legal Value type in RMI-IIOP, subject to the restrictions defined in Section EJB.9.2.9. The primary key class may be specific to an entity bean class (i.e., each entity bean class may define a different class for its primary key, but it is possible that multiple entity beans use the same primary key class). A client that holds a reference to an entity object's remote interface can determine the entity object's identity within its home by invoking the getPrimaryKey() method on the reference. The object identity associated with a reference does not change over the lifetime of the reference. (That is, getPrimaryKey() always returns the same value when called on the same entity object reference.) A client can test whether two entity object references refer to the same entity object by using the isIdentical(EJBObject) method. Alternatively, if a client obtains two entity object references from the same home, it can determine if they refer to the same entity by comparing their primary keys using the equals method. The following code illustrates using the isIdentical method to test if two object references refer to the same entity object: Account acc1 = ...; Account acc2 = ...; if (acc1.isIdentical(acc2)) { acc1 and acc2 are the same entity object } else { acc2 and acc2 are different entity objects } A client that knows the primary key of an entity object can obtain a reference to the entity object by invoking the findByPrimaryKey(key) method on the entity bean's home interface. Note that the Enterprise JavaBeans architecture does not specify "object equality" (i.e., use of the == operator) for entity object references. The result of comparing two object references using the Java programming language Object.equals(Object obj) method is unspecified. Performing the Object.hashCode() method on two object references that represent the entity object is not guaranteed to yield the same result. Therefore, a client should always use the isIdentical method to determine if two entity object references refer to the same entity object. |