The J2EETM Tutorial
Home
TOC
PREV TOC NEXT Search
Feedback

Characteristics of Entity Beans

Entity beans differ from session beans in several ways. Entity beans are persistent, allow shared access, and have primary keys.

Persistence

Because the state of an entity bean is saved in a storage mechanism, it is persistent. Persistence means that the entity bean exists beyond the lifetime of the application or the J2EE server process. If you've worked with databases, you're familiar with persistent data. The data in a database is persistent because it still exists even after you shut down the database server or the applications it services.

There are two types of persistence: bean-managed and container-managed. You declare the persistence type with the deploytool, which stores the information in the entity bean's deployment descriptor.

Bean-Managed Persistence

With bean-managed persistence, the entity bean code that you write contains the calls that access the database. The ejbCreate method, for example, will issue the SQL insert statement. You are responsible for coding the insert statement and any other necessary SQL calls. See A Bean-Managed Persistence Example.

Container-Managed Persistence

If the container manages an entity bean's persistence, it automatically generates the necessary database access calls. For example, when a client creates an entity bean, the container generates a SQL insert statement. The code that you write for the entity bean includes no SQL calls. As a result, the entity bean is independent of any particular datastore, such as a relational database. Because of this independence, the beans are portable across all compliant J2EE servers.

Entity beans with container-managed persistence has several advantages over those with bean-managed persistence:

Shared Access

Entity beans may be shared by multiple clients. Because the clients might want to change the same data, it's important that entity beans work within transactions. Typically, the EJB container provides transaction management. You specify the transaction attributes in the bean's deployment descriptor. You do not have to code the transaction boundaries in the bean-the container marks the boundaries for you. See Transactions for more information.

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 the section, Primary Key Class.

Home
TOC
PREV TOC NEXT Search
Feedback