What is a CMP bean?

Richard Monson-Haefel

CMP: Container-Managed Persistence

A CMP bean is an entity bean whose state is synchronized with the database automatically. In other words, the bean developer doesn't need to write any explicit database calls into the bean code; the container will automatically synchronize the persistent fields with the database as dictated by the deployer at deployment time.

When a CMP bean is deployed, the deployer uses the EJB tools provided by the vendor to map the persistent fields in the bean to the database. The persistence fields will be a subset of the instance fields, called container-managed fields, as identified by the bean developer in the deployment descriptor.

In the case of a relational database, for example, each persistent field will be associated with a column in a table. A bean may map all its fields to one table or, in the case of more sophisticated EJB servers, to several tables. CMP are not limited to relational database. CMP beans can be mapped to object databases, files, and other data stores including legacy systems.

With CMP, the bean developer doesn't need to write any database access logic into the bean, but bean is notified by the container when its state is synchronized with the database. The container notifies the bean using the ejbLoad( ) and ejbStore( ) methods.

The ejbLoad( ) method alerts the bean that its container-managed fields have just been populated with data from the database. This gives the bean an opportunity to do any post processing before the data can be used by the business methods. The ejbStore( ) method alerts the bean that its data is about to be written to the database. This give the bean an opportunity to do any pre-processing to the fields before they are written to the database. The below bean code demonstrates how these method might be employed on a Customer CMP entity bean.


public class CustomerBean implements javax.ejb.EntityBean {
      
         // container-managed fields
         public long customerNumber;
         public String lastName;
         public String firstName;

         // not a container-managed field
         public PersonName name = null;

         // business methods
         public PersonName getName(  ){
                return name;
         }
         public void setName(PersonName name){
               name = name;
         }
         // called just after container-managed fields are updated from database
         public void ejbLoad( ){
              name = PersonName(firstName, lastName);
         }
         // called just before container-managed fields are written to database
         public void ejbStore( ){
               lastName  = name.getLastName( );
               firstName = name.getFirstName( );
        }
... 
}

In the case of the Customer bean the ejbLoad( ) and ejbStore( ) are used to wrapper the name fields (lastName and firstName) in a PersonName object which is more user friendly for the bean client. The ejbLoad( ) method wrappers the fields so that the getName( ) method access an up-to-date copy of the name data. The ejbStore( ) method unwraps the name data and places them back into the container-managed fields so that the database is updated with the most recent changes.

The ejbLoad( ) and ejbStore( ) methods are typically called just before a transaction begins and when a transaction is about to commit. This ensures that the container-managed fields are synchronized with the database when the bean is in use. Some EJB servers will optimize the container so that data is not written to the database unless its dirty; that container-managed fields have been changed.

CMP is the most difficult EJB feature for vendors to support. It requires very sophisticated tools for mapping bean fields to a data source and generating the underlying stubs and skeletons to support this mapping. CMP, however, provide a unique opporutnity for organizations developing distributed object systems. With CMP, a shrink-wrapped bean ( a bean developed by a third party) can be deployed and mapped into an organization's existing database. Obviously, this is an idyllic scenario, but not unreasonable. CMP entity beans exemplify the spirit of component technology; the reuse of components across projects, organizations, and industries.

Note: CMP beans use the ejbLoad( ) and ejbStore( ) callback methods differently than the Bean-Managed Persistence (BMP) beans. See Java:API:EJB:EntityBean:BMP for details on how BMP beans use these methods.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.