Entity Beans

 

Overview

An entity bean represents a business object, such as a buyer, seller, invoice, or receipt. Each entity bean persists data to an underlying table in a relational database, and each instance of the bean corresponds to a row in that table.

Entity beans differ from session beans in that they:

  1. Are persistent
  2. Allow shared access amoung multiple clients
  3. Have primary keys (object identifiers)
  4. Have relationships with other entity beans

Beans persist their data using two methods:

bean-managed persistence The entity bean code that you write contains the calls that access the database.
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.

Because entity beans are shared amongst multiple clients, they use transactions. The EJB container generally provides transaction management, with transaction attributes defined in the bean's deployment descriptor.

 

Container-Managed Persistence

With container-managed persistence the EJB container handles all communication with the database. The bean's code contains no database access (SQL) calls.

As a result, the bean's code is not tied to a specific persistent storage mechanism (database). Because of this flexibility, even if you redeploy the same entity bean on different J2EE servers that use different databases, you won't need to modify or recompile the bean's code.

 

Abstract Schema

To generate the data access calls, entity beans provide an abstract schema to the container. Part of an entity bean's deployment descriptor, the abstract schema defines the bean's persistent fields and relationships. The term abstract distinguishes this schema from the physical schema of the underlying data store.

Abstract schemas is referenced by queries written in the Enterprise JavaBeans Query Language ("EJB QL"). For an entity bean with container-managed persistence, define an EJB QL query for every finder method (except findByPrimaryKey). The EJB QL query determines the query that is executed by the EJB container when the finder method is invoked.

 

Persistent Fields

The persistent fields of an entity bean are stored in the underlying data store. Collectively, these fields constitute the state of the bean. At runtime, the EJB container automatically synchronizes this state with the database. During deployment, the container typically maps the entity bean to a database table and maps the persistent fields to the table's columns.

A CustomerEJB entity bean, for example, might have persistent fields such as firstName, lastName, phone, and emailAddress. In container-managed persistence, these fields are virtual. You declare them in the abstract schema, but you do not code them as instance variables in the entity bean class. Instead, the persistent fields are identified in the code by getters and setters.

 

Relationship Fields

A relationship field is like a foreign key in a database table--it identifies a related bean. Like a persistent field, a relationship field is virtual and is defined in the enterprise bean class with access methods. But unlike a persistent field, a relationship field does not represent the bean's state.

EJB QL queries often navigate across relationships. The direction of a relationship determines whether a query can navigate from one bean to another. For example, a query can navigate from LineItemEJB to ProductEJB, but cannot navigate in the opposite direction. For OrderEJB and LineItemEJB, a query could navigate in both directions, since these two beans have a bidirectional relationship.