# Bean-Managed Persistence

When you use container-managed persistence (CMP) for entity beans, the EJB container handles the interaction with the underlying database. The EJB container ensures synchronization of data just prior and just after a business method executes (see The Life Cycle of an Entity Bean), it deletes the underlying database record when the entity bean instance is deleted, for an ejbCreate method it inserts the data in the database, it automatically generates the findByPrimaryKey method and handles the database query to find a database record, and it interprets EJB QL and WebLogic QL queries on finder and select methods to generate the corresponding database queries. In most cases entity bean development is done using CMP.

In contrast, when you use bean-managed persistence (BMP) for entity beans, the EJB container will still provide a numbers of services (such as transaction management) but leaves the persistence management up to the bean class. Bean-managed persistence might be necessary when your bean represents an unusual set of data and/or interacts with a legacy system for data storage. The current topic introduces some of the main difference between CMP and BMP, and some of the main differences developing CMP entity beans in WebLogic. For detailed information on CMP bean development, see your favorite J2EE documentation.

## Main Differences Between CMP and BMP

Compared to CMP, when you develop BMP entity beans you must take into account the following:

- You are responsible for synchronizing the entity bean instance and the underlying database record before and after a business method invocation. To do so you must implement the callback methods ejbLoad and ejbStore. (The EJB container invokes the various callback methods for CMP and BMP entity beans alike; see The Life Cycle of an Entity Bean.)
- The ejbCreate method(s) must take care of storing the new data in the database.
- The findByPrimaryKey method must be explicitly defined and must take care of locating the data corresponding to the (compound) primary key in the database.
- Removing the data from the database when a bean instance is removed must be handled by the bean class. To do so you must implement the ejbRemove callback method.
- Query methods cannot be implemented using EJB QL or WebLogic QL. Instead the finder or select method(s) must define the database queries.

## BMP Implementation

When you define a CMP entity bean in WebLogic, you should take into account the following:

- To create a new BMP entity bean file, the first step is creating it as you would a CMP entity bean (see How Do I: Create an Enterprise JavaBean?). When the file is created and opened, go to source view and add persistence-type="bmp" to the @ejbgen:entity tag. When you close and re-open the file, you will notice that design view is no longer available and, correspondingly, that the various wizards to for instance add an entity relation or a create method are no longer available.
- You need to add an @ejbgen:resource-ref tag to the datasource (the database). Because the EJB container doesn't handle persistence, you should remove the attributes data-source-name and table-name from the @ejbgen:entity tag.
- The database table for the BMP entity bean is not created for you by WebLogic. You must define the table in the database.
- You must define the property fields and accessor methods.
- You must define the findByPrimaryKey method and the various callback methods as mentioned in the previous section.
- WebLogic will still generate the various interfaces for you. To that effect, you also still use the appropriate @ejbgen tags to ensure that a particular method is added to the appropriate interface. The findByPrimaryKey method will automatically be added to the defined home interface(s).

An implemented BMP entity bean can be found in the BMP Entity Bean Sample.

Related Topics

BMP Entity Bean Sample

The Life Cycle of an Entity Bean

@ejbgen:entity Annotation

@ejbgen:resource-ref Annotation