Entity Beans   «Prev  Next»

Lesson 8The entity bean lifecycle
Objective Describe the entity bean lifecycle.

Entity Bean Lifecycle

The approach to entity management has significantly evolved from legacy entity to beans to the following process.
  • Shift Towards CDI:
    • Jakarta EE 10 emphasizes Contexts and Dependency Injection (CDI).1 This means a move towards a more unified component model.
    • The focus is on leveraging CDI for managing component lifecycles, which affects how entity-like behaviors are handled.2
    • While the "entity bean lifecycle" as it existed in older EJB specifications has changed, the need for managing persistent data and its lifecycle remains.
  • Jakarta Persistence API (JPA):
    • JPA is the modern standard for object-relational mapping in Jakarta EE.3
    • JPA provides mechanisms for managing entities, including their lifecycle, persistence, and relationships.4
    • So, persistence and entity management are very much present, but they are handled through JPA, which is well integrated with CDI.
  • EJB Evolution:
    • The role of traditional Entity Beans has diminished. JPA has become the preferred way to handle persistent entities.
    • Jakarta EE 10 aims to align various component models with CDI, leading to a more consistent development experience.5
    • Therefore, the entity bean lifecycle of older java EE specifications has been replaced by the entity lifecycle management provided by JPA, and the component management provided by CDI.

In summary, while the classic "entity bean lifecycle" is not the same, the functionality it provided is now handled through a combination of JPA and CDI. This represents a modernization and streamlining of the Jakarta EE platform.


Look at the diagram below and ask yourself what is persistent.
What is persistent
The uploaded image represents an Enterprise JavaBeans (EJB) Entity Bean Architecture, showing the interaction between clients, an EJB object, an entity bean, and a database management system (DBMS). Here’s a breakdown of the components:
  1. Client-A and Client-B (Left side, Yellow boxes)
    • These are external clients (could be standalone Java applications, web applications, or other enterprise applications) that interact with the EJB system.
    • They send requests to access business logic or data.
  2. EJBObject (Middle, Red box)
    • This represents the remote interface for the entity bean.
    • It acts as a proxy that clients use to interact with the entity bean.
    • It manages client requests and forwards them to the entity bean.
  3. Entity Bean (Middle-right, Blue box)
    • This is the actual enterprise bean that represents a business entity.
    • It holds instance variables that represent fields mapped to a database.
    • The entity bean is responsible for persisting data to the database.
  4. DBMS (Database Management System) (Right side, Cylinder)
    • This is where the entity bean’s data is stored.
    • The entity bean interacts with the DBMS to read and write persistent data.
Interaction Flow:
  1. Clients (Client-A and Client-B) interact with the EJBObject.
  2. The EJBObject forwards requests to the Entity Bean.
  3. The Entity Bean manages the instance variables and performs transactions with the DBMS for persistence.
  4. Data flows back to the clients through the same path.
This diagram illustrates the Entity Bean Lifecycle in an EJB-based application, where entity beans handle persistent data storage and retrieval using a relational database. What is persistent


EJBObject is a remote proxy in EJB 1.1

In the EJB 1.1 specification, the EJBObject is NOT the actual bean or the underlying data. Instead, it serves as a remote proxy that provides a client-accessible interface to interact with an Enterprise JavaBean (EJB).
Understanding the Role of EJBObject in EJB 1.1:
  1. EJBObject is a Remote Interface Proxy
    • It acts as an intermediary between the client and the actual enterprise bean.
    • It provides remote method invocation (RMI) capabilities, enabling clients to call methods on the EJB as if they were local.
  2. The Actual Bean is Separate from the EJBObject
    • The real business logic and instance variables reside within the bean implementation class (Session Bean or Entity Bean).
    • The EJB container manages the lifecycle of these beans.
  3. The Underlying Data is Stored in a Database
    • For Entity Beans, the actual data is stored in a relational database (DBMS).
    • The Entity Bean’s instance variables map to database fields (in Container-Managed Persistence (CMP)) or are manually managed by the developer (Bean-Managed Persistence (BMP)).

Key Takeaways:
  • EJBObject ≠ Actual Bean → It is a remote proxy interface.
  • EJBObject ≠ Underlying Data → The data is stored in the database and managed via persistence.
  • Clients Interact with EJBObject → It forwards method calls to the actual bean instance.

Diagram Representation: (Client) → EJBObject → EJB Instance (Business Logic) → Database (Persistence)

The Bean Instance can be discarded

The bean instance, can be discarded by the container because it only represents the underlying persistent data. As long as the container remembers the primary key, it can recreate the bean instance and load the data. Thus, an entity bean instance for existing persistent data can be in one of the following states:
  1. It does not exist, but the underlying data does.
  2. It is pooled. An instance that is pooled is not associated with any particular entity object.
  3. Ready state. An instance in the ready state is assigned to a specific piece of persistent data.

It shows a single ready instance, a pool of instances unassociated with any data, and an EJBObject with no bean instance currently in existence. Any invocations on the methods in the EJBObject (that does not currently have an instance) causes the container to take an instance from the pool and load its data before the actual method on the bean is invoked. This is very efficient, as it allows the container to store away instances that are no longer associated with persistent data, and to reuse them when required. It saves having to newInstance() the bean each time, which, as we know, is expensive.

Entity Bean Life Cycle Quiz

Take a moment to test your knowledge on entity bean persistence and lifecycle by clicking the Quiz link below
Entity Bean Life Cycle - Quiz
The next lesson reviews the contents of this module.

SEMrush Software