CONTENTS | PREV | NEXT | INDEX J2EE BluePrints



5.2 Enterprise Beans as J2EE Business Objects

As we discussed in the previous section, business objects need to provide some generic services to clients, such as support for transactions, security, and remote access, These common services are very complex in nature and are outside the domain of the business logic required to implement an application. To simplify development, enterprise applications need a standard server-side infrastructure that can provide such services.

The EJB tier of the J2EE platform provides a standard server-side distributed component model that greatly simplifies the task of writing business logic. In the EJB architecture, system experts provide the framework for delivering system-level services and application domain experts provide the components that hold only business-specific knowledge. The J2EE platform enables enterprise developers to concentrate on solving the problems of the enterprise instead of struggling with system-level issues.

To use the services provided by the J2EE platform, business objects are implemented by EJB components, or enterprise beans. There are two primary kinds of enterprise beans: entity beans and session beans. Session beans are intended to be private resources used only by the client that created them. For this reason, session beans, from the client's perspective, appear anonymous. In contrast, every entity bean has a unique identity which is exposed as a primary key. Later sections in this chapter discuss each type of enterprise bean in detail.

In addition to components, the EJB architecture defines three other entities: servers, containers, and clients. Enterprise beans live inside EJB containers, which provide life cycle management and a variety of other services. An EJB container is part of an EJB server, which provides naming and directory services, email services, and so on. When a client invokes an operation on an enterprise bean the call is intercepted by its container. By interceding between clients and components at the method call level, containers can manage services that propagate across calls and components, and even across containers running on different servers and different machines. This mechanism simplifies development of both components and clients.


5.2.1 Enterprise Beans and EJB Containers

The EJB architecture endows enterprise beans and EJB containers with a number of unique features that enable portability and reusability:

5.2.1.1 Home Interface

The home interface provides methods for creating and removing enterprise beans. This interface must extend javax.EJB.EJBHome. The enterprise bean's home interface allows a client to do the following:

In addition, the home interface of an entity bean provides methods for finding existing entity bean instances within the home. A client that knows the primary key of an entity object can obtain a reference to the entity object by invoking the findByPrimaryKey method on the entity bean's home interface.

5.2.1.2 Remote Interface

The remote interface defines the client view of an enterprise bean--the set of business methods available to the clients. This interface must extend javax.ejb.EJBObject. An EJBObject supports:

The javax.ejb.EJBObject interface defines the methods that allow clients to perform the following operations on a reference to an enterprise bean instance:

5.2.1.3 Enterprise Bean Class

The enterprise bean class is the second part of the mechanism that allows for container-managed services in the EJB architecture. It provides the actual implementation of the business methods of the bean. It is called by the container when the client calls the corresponding methods listed in the remote interface. This class must implement the javax.ejb.EntityBean or javax.ejb.SessionBean interface.

In addition to business methods, the remote interface and enterprise bean class also share responsibility for two specialized categories of methods: create methods and finder methods. The create methods provide ways to customize the bean at the time it is created, and the finder methods provide ways to locate a bean.

For each create method listed in the home interface, the bean class implements the corresponding ejbCreate method. For each finder method listed in home interface, the bean class provides the corresponding ejbFindBy... method. The enterprise bean class must also provide implementations of the methods listed in the interface it extends. A developer can choose to provide empty implementations of any methods in the interface that aren't required for the specific purposes of a bean.

Figure 5.1 illustrates the implementation of the client view of an enterprise bean.

Figure 5.1 Implementation of Client View of Enterprise Beans

The following two sections contain in-depth discussions of the properties and uses of entity and session beans.



CONTENTS | PREV | NEXT | INDEX
Copyright © 2001 Sun Microsystems, Inc. All Rights Reserved.