EJB Entity beans

What are Entity beans? Explain the need of entity beans.

Entity beans are objects that represents a persistence storage mechanism. The objects can be a customer, product, account etc. Each entity bean has underlying table in a relational database and each row in the table represents the instance of the bean.

Need of entity beans:

1. When it is needed by multiple clients.

2. When any action from the database side.

3. When a transaction is to be performed from the client.

4. When the enforcement of accuracy and integrity is needed.

Explain the types of Entity beans. Explain their uses.

There are two types of entity beans.:

1) Container Managed Persistence (CMP)
2) Bean Managed Persistence (BMP)

CMP can be used when:

- No code for database code is needed(by placing the specific accessibility is provided by XML file).

- The code reusability is needed.

- The speed of development by creation of the database access code by the developers.

BMP can be used when:

- Synchronizing the state of the database directly by the container.

- To provide the flexibility for the bean developer to perform the persistence operations.

- The needs of the query exceeds the capabilities of EJB QL.

- The persistent storage is a non database system or is a legacy database system that does not supports for CMP.

Explain the steps involved in developing Entity beans.

1. Select which bean is to be used – BMP or CMP entity bean

2. Create the required entity bean.

3. Author the source code for the entity bean, which involves developing the following:

a. The component interface(s)
b. The home interface(s)
c. The bean class
d. Any helper classes required by the entity bean

4. Develop the deployment descriptors for the beans.

5. Create the database tables. (If your database tables already exist, you can omit this step)

6. Establish the Object Relation mapping.

7. Compile and archive all the EJB components into a JAR file.

8. Archive the complete application into an EAR file.

9. Make the deployment of the application onto the J2EE engine.

10. Start the application.

What are the steps involved in deployment of CMP and BMP entity beans.

The purpose of deployment of entity bean is to make them available for the client applications.

Requirements before deploying:

1. Author and compile the various entity beans that are to be deployed. This includes bean class, remote interfaces, home interfaces, and the primary key class, if required.

2. Get the mapping done for the entity beans for the appropriate database tables and save the mapping information that is a deployable XML file.

3. Assemble the entity beans into a .jar or .ear file

Steps involved in deployment of entity beans:

There are three steps in deploying the entity beans.

1. Configuration :

- Specifying the number of properties for the bean.
- The usage of persistence mechanism and the needed information to do so.

2. Code generation :

- The information from the configuration phase is used by the different tools to generate the classes for the required beans.
- This process may include helper classes related to transactions, persistence, and security.
- The implementations of EJBHome and EJBObject, and the stubs and skeletons required for RMI-IIOP.

3. Installation :

- The EJB server is started and make the beans available to the clients applications.

Explain the lifecycle of the entity beans.

The time when the entity bean starts instantiating and till it takes the time for garbage collection is the life cycle of an entity bean.

The life cycle has three states, namely

1. Does not exist state
2. Pooled state
3. Ready state

1. Does not Exist State :

- The life of a bean’s instance starts with a set of files. These files include remote interface, interface, deployment descriptor, primary key and the needed files at the time of deployment.

- In this state, no instances of the bean exists.

2. Pooled State :

- At the time of starting the EJB server,with the help of the method Class.newInstance() , all the instances are created and placed in a pool. The default values of the persistent fields are set. The bean class should never contain the constructor. Therefore the default no-argument must be available in the container.

- Before placing the instances in the pool, an instance EntityContext is assigned by the container which assigns and implements the setEntityContext() method by the bean class, followed by entering into the instance pool.

- Now the bean instance is available for the client requests and become active soon after receiving the requests. The bean container provides different levels of access at every stage of the bean’s life cycle.

3. The Ready State:

- Once the bean reaches this state, it can accept the requests from the client. The state of a beans instance reaches to ready state once the container assigns it to an EJB object.

- This assignment occurs in two different circumstances:

1. When the new entity bean is being created.
2. When the container is activating an entity bean.

4. End of the Life Cycle :

- The life of a bean’s instance comes to an end when the container decides to remove it from the pool and allows it to be garbage collected.

- This may happen when the container decides to reduce the number of instances from the pool in order to conserve the resources. So that the highest performance can be achieved.
EJB 2.0 Entity Model
EJB 2.0 Entity Model - Explain the advantages of EJB 2.0 container-managed persistence over bean-managed persistence, Explain the types and role of abstract methods in EJB 2.0 persistence, Explain the structure of EJB query language (EJB QL)
EJB Asynchronous communication
EJB Asynchronous communication - What is a message-driven bean?, Explain the need for asynchronous communication, What is the JMS messaging?, Explain the JavaMail API. Explain how it is used to compose and dispatch a mail.
EJB environment
EJB environment - What is the Enterprise Bean Environment?, Explain the EJB environment Entries, Explain the steps involved in accessing one enterprise bean from other.
Post your comment