Professional Documents
Culture Documents
1 TABLE OF CONTENTS
2 Business Logic and Business Objects.................................................................................................................... 2
2.1 Common Requirements of Business Objects ............................................................................................... 3
2.1.1 Maintain State ...................................................................................................................................... 3
2.1.2 Operate on Shared Data....................................................................................................................... 3
2.1.3 Participate in Transactions ................................................................................................................... 3
2.1.4 Remain Available to Clients .................................................................................................................. 3
2.1.5 Provide Remote Access to Data ........................................................................................................... 3
2.1.6 Control Access ...................................................................................................................................... 3
2.1.7 Service a Large Number of Clients ....................................................................................................... 3
3 Enterprise Beans as J2EE Business Objects .......................................................................................................... 4
3.1 Enterprise Beans and EJB Containers ........................................................................................................... 4
3.1.1 Home Interface .................................................................................................................................... 4
3.1.2 Component Interface ........................................................................................................................... 5
3.1.3 Enterprise Bean Class ........................................................................................................................... 5
4 Remote and Local Client Views ............................................................................................................................ 5
4.1 Guidelines for Using Local or Remote Client Views ..................................................................................... 5
4.2 Entity Beans and Local Client Views ............................................................................................................. 6
5 Entity Beans .......................................................................................................................................................... 6
5.1 Guidelines for Using Entity Beans ................................................................................................................ 6
5.1.1 Representing persistent data ............................................................................................................... 6
5.1.2 Representing objects with clearly-defined identity ............................................................................. 6
5.1.3 Providing concurrent access by multiple clients .................................................................................. 6
5.1.4 Providing robust, long-lived persistent data management ................................................................. 7
5.1.5 Persisting data in a portable manner ................................................................................................... 7
5.1.6 Providing access through queries ........................................................................................................ 7
5.1.7 Simplifying transaction handling .......................................................................................................... 7
5.2 Entity Bean Persistence ................................................................................................................................ 7
5.3 When to Use Bean-Managed Persistence .................................................................................................... 7
6 Session Beans ....................................................................................................................................................... 8
6.1 Stateful Session Beans.................................................................................................................................. 8
6.1.1 Uses of Stateful Session Beans ............................................................................................................. 8
6.2 Stateless Session Beans ................................................................................................................................ 8
6.2.1 Uses of Stateless Session Beans ........................................................................................................... 9
7 Message-Driven Beans ......................................................................................................................................... 9
7.1 Uses of Message-Driven Beans .................................................................................................................... 9
8 Design Guidelines ............................................................................................................................................... 10
8.1 Remote versus Local Client Access for Entity Beans .................................................................................. 10
8.1.1 First Way............................................................................................................................................. 11
8.1.2 Second Way ........................................................................................................................................ 11
8.2 Session Beans as a Facade to Entity Beans ................................................................................................ 11
8.3 Master-Detail Modeling Using Enterprise Beans ....................................................................................... 11
8.4 Representing References to Entity Beans .................................................................................................. 12
8.4.1 Use a container-managed relationship .............................................................................................. 12
8.4.2 Store a reference to a home or component interface ....................................................................... 12
8.4.3 Use the referenced entity bean’s primary key................................................................................... 12
8.4.4 Do not pass this as a reference to an enterprise bean. ..................................................................... 12
The following discussion demonstrates how to define the structure and behavior of a business object from the
requirements imposed by the business problem it addresses.
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 business-
specific knowledge. The J2EE platform enables enterprise developers to concentrate on solving the problems of
the enterprise instead of expending their efforts on system-level issues.
The Enterprise JavaBeans architecture defines components—called enterprise beans—that allow the developer
to write business objects that use the services provided by the J2EE platform.
There are three kinds of enterprise beans: session beans, entity beans, and message-driven beans:
Session beans are intended to be private resources used only by the client that creates them. For this
reason, session beans, from the client’s perspective, ap-pear anonymous.
Entity beans are components that represent an object-oriented view of some entities that are stored in
persistent storage, such as a database. In contrast to session beans, every entity bean has a unique
identity that is exposed as a primary key.
Message-driven beans are new to the EJB 2.0 architecture supported in the J2EE 1.3 platform. Message-
driven beans are components that process asynchronous messages delivered via the Java Message
Service (JMS) API. Message-driven beans, by implementing a JMS message listener interface, can
asynchronously consume messages sent to a JMS queue or topic.
The home interface for an enterprise bean with a remote client view must extend
javax.ejb.EJBHome. The home interface for an enterprise bean with a local client view extends
javax.ejb.EJBLocalHome.
Generally, the enterprise bean’s remote home interface allows a client to do the following:
The javax.ejb.EJBObject and the javax.ejb.EJBLocalObject interfaces define the methods that
allow clients to perform the following operations on a reference to an enterprise bean object:
Use remote interfaces when the distribution of components (or their potential distribution)
requires location independence in the deployment environment.
Use remote interfaces when loose coupling between a bean and its client is desirable.
Use remote interfaces to ensure that parameters are passed between a bean and a client using
pass-by-value semantics rather than pass-by-reference semantics.
Use remote interfaces for entity beans that require only coarse-grained access to the underlying
data objects.
The use of local interfaces for enterprise beans may be better suited to some applications. Consider
using local interfaces for session and entity beans under these circumstances:
Use local interfaces for a session or entity bean if they are required to be located in the same
container as their clients. For local interfaces to be used, the session or entity bean must be
deployed in the same JVM as the client.
Use local interfaces when tight coupling between a client and a bean is desirable.
Use local interfaces when pass-by-reference semantics is preferable for parameter passing
between a client and the bean. This is typically done to achieve higher performance by avoiding
the overhead of object copying.
Use local interfaces for entity beans that expose fine-grained access to the underlying data
objects.
5 ENTITY BEANS
An entity bean represents an object view of business data stored in persistent storage or an existing application.
The bean provides an object wrapper around the data to simplify the task of accessing and manipulating it. This
object view of data lends itself to software reuse
Directly implementing persistence in the enterprise bean class or in one or more helper objects
provided with the enterprise bean class (bean-managed persistence)
Delegating the handling of its persistence to its container (container-managed persistence)
Use bean-managed persistence when you need exact control over the database schema or need
to customize code to match a specific legacy database schema.
Use bean-managed persistence when it is important that the application be very finely tuned
against the database that is in use.
Use bean-managed persistence when portability is not an issue. Even in this case, however, it is
recommended that data access objects (DAOs) be used. DAOs better enable the bean to be
adapted to a different database schema or to evolve into an entity bean with container-managed
persistence at a later date. See Section 5.7.5 on page 161.
Use bean-managed persistence when the query needs of the application exceed the current
capabilities of EJB QL. While EJB QL will continue to develop, it is not yet able to express a
number of queries that are expressible in SQL.
Use bean-managed persistence when your persistent store is not a database system or is a legacy
database system that is not likely to be supported for container-managed persistence.
6 SESSION BEANS
Session beans are used to implement business objects that hold client-specific busi-ness logic. The state of such a
business object reflects its interaction with a particular client and is not intended for general access. Therefore, a
session bean typically exe-cutes on behalf of a single client and cannot be shared among multiple clients. A
session bean is a logical extension of the client program that runs on the server and contains information specific
to the client. In contrast to entity beans, session beans do not directly represent shared data in the database,
although they can access and update such data. The state of a session object is non-persistent and need not be
written to the database.
7 MESSAGE-DRIVEN BEANS
A message-driven bean is a new type of enterprise bean introduced in EJB 2.0. A message-driven bean allows J2EE
applications to receive JMS messages asynchronously. (Asynchronous messaging allows applications or
components to communicate with other applications or components by exchanging messages in such a way that
senders are independent of receivers. The sender sends its message and does not need to wait for the receiver to
receive or process the message.)
A message-driven bean’s business logic may key off the contents of the received message, or it may be driven by
the mere fact of receiving the message.
8 DESIGN GUIDELINES
While you are free to write your application and enterprise bean code according to your own needs, we do
recommend certain guidelines.
Keep the code in enterprise beans as “client-neutral” as possible. Enterprise beans are meant to contain
business logic that can be used by many client types. Methods in enterprise beans that serve only one
client type make any logic within that method inaccessible to other client types. Code that is specific for a
particular client type belongs with the software managing that client type. In particular, Web-tier and
HTTP-related functions and data do not belong in an enterprise bean.
Keep as much business logic as possible in the EJB tier. By doing so, you take advantage of the EJB
container services and simplify your programming ef-fort.
Use stateful session beans to manage conversational state rather than managing conversational state in
the client or Web tier. As with the previous point, this leverages the advantages of enterprise beans.
You may also need to consider the services provided by enterprise beans and the EJB container, and decide
whether these services benefit your application.
You should take into consideration these EJB services and advantages:
The EJB architecture handles such system services as transaction manage-ment, security, scalability,
persistent data access, distributed processing, and concurrency. An enterprise bean developer does not
have to include code to handle these services. As a developer, you can focus on the application and
business logic.
Enterprise beans support multiple types of components.
Enterprise beans support applications with a complex series of operations and accumulation of
conversation state over time.
Enterprise beans are portable across hardware platforms, operating systems, server implementations,
and databases.
Enterprise beans can be easily customized in the runtime environment.
Enterprise beans are reusable software modules.
Entity beans represent an object-oriented view of data and provide business logic to manipulate this
data. In an enterprise environment, entity beans often need to be shared among different applications
representing different tasks. In such cases, use of application-specific stateful session beans to manage
the inter-action of various entity beans provides a simpler interface to the client by giving the client a
central point of entry. The client always interacts with this session bean and is unaware of the existence
of other entity beans in the system. However, if the client interacts with only a few entity beans in a
relatively simple way, the entity beans can be exposed directly.
A master-detail relationship is a one-to-many type relationship among data sets. A set of purchase orders
and a set of line items belonging to each purchase order is an example of a master-detail relationship. An
application can use this master-detail relationship to enable users to navigate through the purchase order
data and see the detail data for line items only when needed.
Entity beans with container-managed persistence are the preferred way to implement master-detail
relationship modelling, since they support container-managed relationships. Further, container-managed
relationships provide a cascade-delete facility that automatically enables the lifetime of the detail object
to be dependent on the lifetime of the master, capturing an important aspect of this dependency.
8.4 REPRESENTING REFERENCES TO ENTITY BEANS
References between enterprise beans are common in many applications. Entity beans reference other
related beans, stateful session beans may keep references to entity beans in conversational state, and
stateless session beans and message-driven beans may look up and pass references to other beans.
Developers can represent references between entity beans in the following ways: