World's most popular travel blog for travel bloggers.

What are the important features of entity beans. Distinguish between Bean managed and Container managed persistence for Entity Beans. Write the methods required for entity beans.

, , No Comments

Entity Bean Features

An entity bean represents a business object in a persistent storage mechanism. Some examples of business objects are customers, orders, and products. In the J2EE SDK, the persistent storage mechanism is a relational database. Typically, each entity bean has an underlying table in a relational database, and each instance of the bean corresponds to a row in that table.

Important Features of Entity Beans

These are the key features of entity beans:
  • Persistence—Entity bean persistence can be managed by the EJB container, or the bean itself. If a bean uses container-managed persistence, the EJB container automatically generates the necessary database access calls. The code that you write for the entity bean does not include these calls. With bean-managed persistence, you must write the database access code and include it in the bean.
  • Shared Access—Throughout its lifecycle, an entity bean instance can support multiple clients, although not at the same time. Because the clients might want to change the same data, it is important that entity beans work within transactions. Typically, the EJB container provides transaction management. In this case, you specify the transaction attributes in the bean's ejb-jar.xml file that control how transactions are managed. You do not have to code the transaction boundaries in the bean—the container marks the boundaries for you. For information about transaction management, see Features and Design Patterns.
  • Primary Key—Each entity bean has a unique object identifier. A customer entity bean, for example, might be identified by a customer number. The unique identifier, or primary key, enables the client to locate a particular entity bean. For more information, see Using Container-Managed Relationships (CMRs).
  • Relationships—Like a table in a relational database, an entity bean may be related to other entity beans. You implement relationships differently for entity beans with bean-managed persistence and for those with container-managed persistence. With bean-managed persistence, the code that you write implements the relationships. But with container-managed persistence, the EJB container takes care of the relationships for you. For this reason, relationships in entity beans with container-managed persistence are often referred to as container-managed relationships. For more information, see Using Cascade Delete for Entities in CMRs.

Difference Between Bean-Managed and Container-Managed Beans


There are two methods for managing the persistent data within an entity bean: bean-managed and container-managed persistence. The main difference between bean-managed and container-managed persistent beans is defined by who manages the persistence of the entity bean's data.

In practical terms, the following table provides a definition for both types and a summary of the programmatic and declarative differences between them:

Bean-Managed Persistence  Container-Managed Persistence  

Persistence management  

You are required to implement the persistence management within the ejbStore and ejbLoad EntityBean methods. These methods must contain logic for saving and restoring the persistent data.
For example, the ejbStore method must have logic in it to store the entity bean's data to the appropriate database. If it does not, the data can be lost. See "3. Implementing EntityBean Interface Methods" for an example of implementing bean-managed persistence.  

The management of the persistent data is done for you. That is, the container invokes a persistence manager on behalf of your bean.
You use ejbStore and ejbLoad for preparing the data before the commit or for manipulating the data after it is refreshed from the database. The container always invokes the ejbStore method right before the commit. In addition, it always invokes the ejbLoad method right after reinstating CMP data from the database.  

Finder methods allowed  

The findByPrimaryKey method and any other finder method you wish to implement are allowed.  

Only the findByPrimaryKey method and a finder method for the where clause are allowed.  

Defining CMP fields  

N/A  

Required within the EJB deployment descriptor. The primary key must also be declared as a CMP field.  

Mapping CMP fields to resource destination.  

N/A  

Required. Dependent on persistence manager.  

Definition of persistence manager.  

N/A  

Required within the Oracle-specific deployment descriptor. See the next section for a description of a persistence manager.  

Callback Methods in EntityBean

Callback Method  Functionality Required  

ejbCreate  

The same functionality as bean-managed persistent beans. You must initialize all container-managed persistent fields, including the primary key.  

ejbPostCreate  

The same functionality as bean-managed persistent beans. You have the option to provide any other initialization, which can involve the entity context.  

ejbRemove  

No functionality for removing the persistent data from the outside resource is required. The persistent manager removes all persistent data associated with the entity bean from the database. You must at least provide an empty implementation for the callback, which means that you can add logic for performing any cleanup functionality you require.  

ejbFindByPrimaryKey 

No functionality is required for returning the primary key to the container. The container manages the primary key--after it is initialized by the ejbCreate method. Thus, the container performs the functionality normally required of this method. You still must provide an empty implementation for this method.  

ejbStore  

No functionaltiy is required for saving persistent data within this method. The persistent manager saves all persistent data to the database for you. However, you must provide at least an empty implementation as the container invokes the ejbStore method before invoking the persistent manager. This enables you to perform any data management or cleanup before the persistent data is saved.  

ejbLoad  

No functionality is required for restoring persistent data within this method. The persistence manager restores all persistent data for you. However, you must provide at least an empty implementation as the container invokes the ejbLoad method after invoking the persistent manager. This enables you to perform any logic to manipulate the persistent data after it is restored to the bean.  

setEntityContext  

Associates the bean instance with context information. The container calls this method after the bean creation. The enterprise bean can store the reference to the context object in an instance variable, for use in transaction management. Beans that manage their own transactions can use the session context to get the transaction context.
You can also allocate any resources that will exist for the lifetime of the bean within this method. You should release these resources in unsetEntityContext.  

unsetEntityContext 

Unset the associated entity context and release any resources allocated in setEntityContext.  

0 comments:

Post a Comment

Let us know your responses and feedback