Entity Beans  «Prev  Next»

Lesson 5 Comparing entity bean instances
ObjectiveWrite the code to compare entity bean instances.

Comparing Entity Bean Instances

Recall from the previous lesson that the relationship between clients and entity beans is many-to-one.
Many clients can access the same entity bean. This is illustrated in the following diagram:

Remote reference communicates with the EJBOject, instantiates a remote instance, and communicates with the relational database.

In the diagram there is one instance and one client.
However, the client has two references to the same instance.
This situation may have arisen by one reference being a return from a create() method and the other from a findByPrimaryKey() method.
The question the client program may need to know is: "Do both references refer to the same instance or not?"

isIdentical()

The EJBObject provides the isIdentical() method for entity beans in the same way it does for session beans.
This method can be used to compare the identities of the beans that have references in your client program. The rule is that references to entity bean instances that refer to the same instance will compare equal when you use isIdentical(). View the diagram below to examine the following client code.


  1. Creates an instance for the customer with the custnum 333 and store the EJBObject reference in variable goofy
  2. Sets the name of the customer to goofy by calling the business method setName().
  3. Finds the entity with the custnum 333 and stores the reference to the EJBObject in bean1.
  4. Accesses the business method getInfo() to retrieve the data.
  5. Using isIdentical(), the reference goofy is compared to bean1. They compare equal, since the two references refer to the same instance.

Comparing Entity Bean References
The output of the code segment is:

bean1 is identical to goofy

Entity Bean Client - Quiz

Click the Quiz link below to test your understanding of the entity bean client.
Entity Bean Client - Quiz
The next lesson introduces the complete code for the Customer client.