Skip Headers

Oracle® Containers for J2EE Enterprise JavaBeans Developer's Guide 10g Release 3 (10.1.3) B14428-01 |
| Home | Solution Area | Contents | Index

Previous | Next |

# Which Type of EJB Should You Use?

This section describes:

- Which Type of Session Bean Should You Use?

- When do you use Bean-Managed versus Container-Managed Persistence?

- What is the Difference Between Session and Entity Beans?

## Which Type of Session Bean Should You Use?

Stateless session beans are useful mainly in middle-tier application servers that provide a pool of beans to process frequent and brief requests.

## When do you use Bean-Managed versus Container-Managed Persistence?

In practical terms, Table 1-19 provides a definition for both BMP and CMP, and a summary of the programmatic and declarative differences between them.

Table 1-19 Comparison of Bean-Managed and Container-Managed Persistence

Management Issues | Bean-Managed Persistence | Container-Managed Persistence
--- | --- | ---
Persistence management | You are required to implement the persistence management within the `ejbStore`, `ejbLoad`, `ejbCreate`, and `ejbRemove 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. | 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 other finder methods are allowed. | The `findByPrimaryKey` method and other finder methods 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. By default,OC4J uses the TopLink persistence manager.

With CMP, you can build components to the EJB 2.0 specification that can save the state of your EJB to any J2EE supporting application server and database without having to create your own low-level JDBC-based persistence system.

With BMP, you can tailor the persistence layer of your application at the expense of additional coding and support effort.

For more information, see:

- "What is an EJB 2.1 CMP Entity Bean?"

- "What is an EJB 2.1 BMP Entity Bean?"

## What is the Difference Between Session and Entity Beans?

The major differences between session and entity beans are that entity beans involve a framework for persistent data management, a persistent identity, and complex business logic. Table 1-20 illustrates the different interfaces for session and entity beans. Notice that the difference between the two types of EJBs exists within the bean class and the primary key. All of the persistent data management is done within the bean class methods.

Table 1-20 Session and Entity Bean Differences

J2EE Subject | Entity Bean | Session Bean
--- | --- | ---
Local interface | Extends `javax.ejb.EJBLocalObject` | Extends `javax.ejb.EJBLocalObject`
Remote interface | Extends `javax.ejb.EJBObject` | Extends `javax.ejb.EJBObject`
Local Home interface | Extends `javax.ejb.EJBLocalHome` | Extends `javax.ejb.EJBLocalHome`
Remote Home interface | Extends `javax.ejb.EJBHome` | Extends `javax.ejb.EJBHome`
Bean class | Extends `javax.ejb.EntityBean` | Extends `javax.ejb.SessionBean`
Primary key | Used to identify and retrieve specific bean instances | Not used for session beans. Stateful session beans do have an identity, but it is not externalized.