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
- 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.
|
0 comments:
Post a Comment
Let us know your responses and feedback