Prev | Next

TOC | Index

J2EETM Developer's Guide

Enterprise Beans

Enterprise beans are server components written in the Java programming language. Enterprise beans contain the business logic for your application. For example, a checkbook client might invoke the debit and credit methods of an account enterprise bean.

There are two types of enterprise beans: session beans and entity beans.

Session Beans

A session bean represents a client in the J2EE server. A client communicates with the J2EE server by invoking the methods that belong to an enterprise bean. For example, an online shopping client might invoke the enterOrder method of its session bean to create an order. A session bean converses with the client, and can be thought of as an extension of the client. Each session bean can have only one client. When the client terminates, its corresponding session bean also terminates. Therefore, a session bean is transient, or non-persistent.

Entity Beans

An entity bean represents a business object in a persistent storage mechanism such as a database. For example, an entity bean could represent a customer, which might be stored as a row in the customer table of a relational database. An entity bean's information does not have to be stored in a relational database. It could be stored in an object database, a legacy application, a file, or some other storage mechanism. The type of storage mechanism depends on the particular implementation of EJB technology. The reference implementation (J2EE SDK) uses a relational database. See the section "Database Access" for more information.

The persistence of an entity bean can be managed by either the entity bean itself, or by the EJB container. Bean-managed persistence requires you to write the data access code in the Bean. For example, a customer entity bean would include the SQL commands to access a relational database via JDBC. Container-managed persistence means that the EJB container handles the data access calls automatically.

Comparing Session and Entity Beans

Although both session and entity beans run in an EJB container, they are quite different. The following table contrasts session and entity beans:

TABLE 1-1 Differences Between Session and Entity Beans

Session Bean

Entity Bean


Performs a task for a client. Represents a business entity object that exists in persistent storage.

May have one client. May be shared by multiple clients.

Not persistent. When the client terminates its session bean is no longer available. Persistent. Even when the EJB container terminates, the entity state remains in a database.

The flexibility of the EJB architecture allows you to build applications in a variety of ways. The following illustration shows how you might create an online shopping application with both session and entity beans. An HTML form displayed in a Web browser accesses a servlet in a Web container. The servlet is the client of a shopping session bean. When the HTML form needs to find a product or enter an order, it instructs the servlet to call the appropriate business methods in the session bean. The session bean is the client of the order, product, and customer entity beans. Because entity beans are persistent, their state is stored in the database.

FIGURE 1-3 Using Session and Entity Beans

Java BeansTM Components and Enterprise Beans

JavaBeans components and enterprise beans are not the same. Although both components are written in the Java programming language, they are not interchangeable. JavaBeans components define a convention for making a Java class instance customizable by design tools, allowing the tools to link these customized objects via events. Enterprise beans implement multi-user, transactional services.

Programming Restrictions for Enterprise Beans

Enterprise beans make use of the services provided by the EJB container, such as life-cycle management. To avoid conflicts with these services, enterprise beans are restricted from performing certain operations:

Database Access

The Enterprise JavaBeans specification does not require an implementation to support a particular type of database. Therefore, the databases supported by different J2EE implementations may vary. See the Release Notes for a list of the databases currently supported by the J2EE SDK.

Both session and entity beans can access a database. The type of enterprise bean you choose depends on your application. You might want to include SQL calls in a session bean under the following circumstances:

You should probably access a database from an entity bean if any of the following conditions are true:

Prev | Next

TOC | Index

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