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.
The image represents the architecture of an Entity Bean in EJB 2.x, illustrating how a client interacts with an Enterprise JavaBean (EJB) running inside an EJB container. Below is an analysis of the different components in the diagram:
Component Breakdown:
  1. Client (Left Side)
    • The client interacts with the Entity Bean through remote references.
    • These remote references allow the client to call methods on the EJB without directly accessing the bean itself.
  2. Container (Middle Section)
    • The container manages the lifecycle of the EJB and acts as an intermediary between the client and the database.
    • It includes two key objects:
      • Home Object: Acts as a factory for creating, finding, and removing entity beans.
      • EJBObject: Represents the remote interface for accessing the entity bean.
  3. Remote Instance (Inside the Container)
    • This is the actual instance of the Entity Bean.
    • The container manages its persistence and interactions with the database.
  4. Database (Right Side)
    • The Entity Bean is mapped to a row in the database.
    • Any updates, reads, or deletions performed on the entity bean reflect in the database.


Interaction Flow:
  • The client calls methods on the Home Object to locate or create entity beans.
  • The Home Object returns a reference to an EJBObject that represents the entity bean.
  • The client then interacts with the EJBObject, which delegates operations to the remote instance managed by the container.
  • The remote instance interacts with the database to persist entity data.

Key Observations:
  • This model was used in EJB 2.x but was deprecated in favor of JPA in EJB 3.0.
  • The complexity of managing home interfaces, remote interfaces, and container-managed persistence (CMP) made it less efficient.
  • Java EE 8 recommends using JPA entities instead of Entity Beans for better scalability and maintainability.
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.


Comparing Entity Bean References

Comparing entity bean references
Customer goofy = home.create("333");
goofy.setName("goofy");

Customer bean1 = home.findByPrimaryKey("333");
System.out.println("Found " + bean1.getInfo());

if (bean1.isIdentical(goofy)) 
    System.out.println("bean1 is identical to goofy");
else
    System.out.println("bean1 is not identical to goofy");

Explanation of the Code:
  1. Creating an Entity Bean Instance
        Customer goofy = home.create("333");
        
    • This creates a new entity bean with primary key "333".
  2. Setting an Attribute
        goofy.setName("goofy");
        
    • Assigns "goofy" as the name of the entity.
  3. Looking Up an Entity Bean
        Customer bean1 = home.findByPrimaryKey("333");
        
    • Retrieves an entity bean from the database using its primary key "333".
  4. Printing Retrieved Entity Information
        System.out.println("Found " + bean1.getInfo());
        
    • Displays the retrieved entity's information.
  5. Checking Identity of Entity Beans
        if (bean1.isIdentical(goofy)) 
            System.out.println("bean1 is identical to goofy");
        else
            System.out.println("bean1 is not identical to goofy");
        
    • The isIdentical() method checks if bean1 and goofy refer to the same entity instance managed by the EJB container.

 

Concept in EJB:
  • The isIdentical() method is specific to Entity Beans in EJB 2.x, used to check if two references point to the same entity in the database.
  • In EJB 3.x and JPA, this concept is replaced by entity identity using equals() and primary key comparison.
Would you like a modern JPA equivalent of this code? Comparing entity bean references


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


 


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.

SEMrush Software