Prev | Next

TOC | Index

J2EETM Developer's Guide
Entity Beans

Characteristics of Entity Beans

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


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 Application Deployment Tool, which stores the information in the entity bean's deployment descriptor.

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.

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 does not include any SQL calls. The container also synchronizes the entity bean's instance variables with the data in the underlying database. These instance variables are often referred to as container-mananged fields. You declare the container-managed fields with the Application Deployment Tool, which enters the list of fields in the deployment descriptor.

Container-managed persistence has two advantages over bean-managed persistence. First, entity beans with container-managed persistence require less code. Second, because the beans don't contain the database access calls, the code is independent of any particular data store, such as a relational database. However, container-managed persistence has several limitations. See the Release Notes for a complete list of limitations.

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 the chapter on 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 on page 66.

Prev | Next

TOC | Index

Copyright © 2000 Sun Microsystems, Inc. All rights reserved.