Sun Logo


Building Enterprise JavaBeans Components

Suntrademark ONE Studio 5 Programming Series

817-2330



Contents

Before You Begin

Before You Read This Book

How This Book Is Organized

Typographic Conventions

Related Documentation

Contacting Sun Technical Support

Sun Welcomes Your Comments

1. Enterprise JavaBeans Concepts and the Sun ONE Studio 5 IDE

The J2EE Architecture

The Roles of EJB Components

The Roles of Application Builders

Inside an EJB Application

The Elements of an Enterprise Bean

Bean Methods

Types of Interfaces

The Bean Class

EJB QL

The Deployment Descriptor

The Work Flow of an EJB Application at Runtime

An Enterprise Bean's Development Life Cycle

The IDE's Support for Enterprise Beans

Developing Enterprise Beans in the IDE

Creating Sets of Related CMP Entity Beans

Providing Transactions

Providing Persistence

Providing Security

Creating Application Clients

Providing for Deployment

Further Reading

2. Design and Programming Issues

Deciding Which Type of Bean You Need

Understanding Session Beans

Deciding When to Use a Stateless Session Bean

Deciding When to Use a Stateful Session Bean

Selecting a Transaction Mode

Understanding the Life Cycle of a Session Bean

Understanding Entity Beans

Taking Advantage of the EJB Container's Services

Understanding the Life Cycle of an Entity Bean

Understanding Sets of Related CMP Entity Beans and Container-Managed Relationships

Understanding Message-Driven Beans

Using Message Sources (Destinations)

Deciding When to Use a Message-Driven Bean

Deciding When Another Bean Type Is Better

Understanding the Life Cycle of a Message-Driven Bean

Using Enterprise Beans in Applications

Using Exceptions to Handle Problems

Working With Deployment Descriptors

Enforcing Security Policies

Declaring Security in Enterprise Beans

Programming Security Into Enterprise Beans

Understanding the Application Servers and Databases

Further Reading

3. Developing Session Beans

Using the EJB Builder With Session Beans

Selecting a Session Bean Type

Stateless or Stateful Session Beans

Container-Managed or Bean-Managed Transactions

Defining a Session Bean

Creating a Package

Starting the EJB Builder Wizard

Generating the Default Session Bean

Looking at a Session Bean's Classes

Expanding the Nodes

Reviewing the Generated Classes

Default Create Method

Life-Cycle Methods

Completing Your Session Bean

Using Recommended Approaches When Working With Enterprise Beans

Completing Create Methods

Completing a Stateless Bean's Create Method

Completing a Stateful Bean's Create Methods

Adding a Create Method to a Stateful Bean

Completing Life-Cycle Methods

Completing the ejbPassivate Method

Completing the ejbActivate Method

Adding Business Methods

Coding Transactions

Understanding Transaction Spans

Specifying Transaction Boundaries and Rollbacks

After Creating Your Session Bean

Further Reading

4. Developing CMP Entity Beans

Using the EJB Builder With CMP Entity Beans

Comparing CMP and BMP Entity Beans

Creating Sets of Related CMP Entity Beans

Defining a CMP Entity Bean

Creating a Package

Having a Data Source Ready

Starting the EJB Builder Wizard

Generating a CMP Entity Bean's Infrastructure

Specifying Persistent Fields From a Database Table

Creating Your Bean's Persistent Fields From Scratch

Looking at a CMP Entity Bean's Classes

Expanding the Nodes

Reviewing the Generated Classes

Default Finder Method

Persistent Fields and Accessor Methods

Primary-Key Class and Required Methods

A CMP Entity Bean's Life-Cycle Methods

Completing Your CMP Entity Bean

Using Recommended Approaches When Working With Enterprise Beans

Defining Create Methods

Adding or Replacing a Primary Key

Creating a New Primary Key

Handling Foreign Keys

Defining Business Methods

Adding Finder Methods

Defining Home Methods

Defining Select Methods

Defining Private Methods

Defining Additional Fields

After Creating Your CMP Entity Bean

Further Reading

5. Developing Sets of Related CMP Entity Beans

Using the EJB Builder With Sets of Related CMP Entity Beans

Creating All Related CMP Entity Beans at Once

Creating a Set of Related CMP Entity Beans One at a Time

Defining a Set of Related CMP Entity Beans

Creating a Package

Preparing to Use a Database or Schema

Starting the EJB Builder Wizard

Generating the Bean Set's Infrastructure

Using a Database Connection

Using a Database Schema Object

Looking at the Components of a CMP Entity Bean Set

Expanding the EJB Module's Node

Reviewing the Generated Classes

Completing Your Set of Related CMP Entity Beans

Using Recommended Approaches When Working With Enterprise Beans

Adding a Bean to the Set

After Creating Your Set of Related CMP Beans

6. Developing BMP Entity Beans

Deciding on an Approach

Building a BMP Entity Bean

Creating a Package

Starting the EJB Builder Wizard

Generating a BMP Entity Bean's Infrastructure

Looking at a BMP Entity Bean's Classes

Expanding the Nodes

Reviewing the Generated Classes

findByPrimaryKey Method

A BMP Entity Bean's Life-Cycle Methods

Completing Your BMP Entity Bean

Using Recommended Approaches When Working With Enterprise Beans

Adding Persistence Logic

Adding a Primary-Key Class

Adding Methods

Defining Create Methods

Adding Finder Methods

Defining Business and Home Methods

After Creating Your BMP Entity Bean

Further Reading

7. Developing Message-Driven Beans

Using the EJB Builder With Message-Driven Beans

Deciding on Transaction Management

Defining a Message-Driven Bean

Creating a Package

Starting the EJB Builder Wizard

Generating the Basic Message-Driven Bean

Looking at a Message-Driven Bean's Components

Expanding the Nodes

Reviewing the Generated Class

Completing Your Message-Driven Bean

Using Recommended Approaches When Working With Enterprise Beans

Completing the onMessage Method

Completing the setMessageDrivenContext Method

After Creating Your Message-Driven Bean

Specifying a Message-Driven Destination

Specifying a Message Selector

Specifying Resources for Client Message-Driven Beans

Specifying Resource Factories

Specifying Resources

Avoiding Pitfalls of Message-Driven Beans

Further Reading

8. Preparing Enterprise Beans for Deployment

Understanding Deployment Information

Looking at a Generated Deployment Descriptor

Changing a Deployment Descriptor

Editing an EJB Module's Deployment Descriptor Directly

Reverting to the EJB Module's Last Generated Descriptor

Using Properties to Edit a Deployment Descriptor

Specifying Bean Properties

Using the Properties Tab

Properties of Entity Beans

Properties of Session Beans

Properties of Message-Driven Beans

Using the References Tab

Specifying EJB Local References

Specifying EJB References

Specifying Environment Entries

Specifying Resource Environment References

Specifying Resource References

Specifying Security-Role References

Using the Sun ONE AS Tab

Setting Server Properties for Individual Session and Entity Beans

Setting Sun ONE AS Properties for Message-Driven Beans

Creating and Configuring an EJB Module

Deciding What Should Go Into an EJB Module

Considering When to Create an EJB Module

Putting Enterprise Beans in an EJB Module

Setting Database-related Properties for CMP Entity Beans

Understanding the Application Server's Generated SQL

Adding Transaction Attributes to an EJB Module

Changing EJB References Within the EJB Module or Application

Overriding a Reference at the Module Level

Overriding a Reference at the Application Level

Adding Extra Files to an EJB Module

Creating an EJB JAR

Creating a J2EE Application

9. Testing Enterprise Beans

Fulfilling the Prerequisites for Testing

Preparing to Deploy to the Application Server

Preparing to Test Beans Against the PointBase Database

Starting PointBase and a Web Browser

Generating Test Objects

Deploying the Test Application to a Server

Deploying and Executing the Test Application in One Step

Executing the Test Application

Using the Test Client to Test Your Beans

Understanding the Test Client Page

Testing the Sample Bean's Home Interface

Testing the Sample Bean's Business Method

Creating New Testing Classes

Making Changes After Deployment

Preparing to Test: Some Variations

If You Want to Test a CMP or BMP Bean

If You Want to Test a Bean With EJB References

Adding Remote Interfaces to a Bean

Using the Customizer to Add Remote Interfaces

Using Property Sheets to Add Remote Interfaces

A. Working With Enterprise Beans

Using Recommended Approaches When Editing Beans

Working Through the Logical Node

Using the Customizer or Property Sheet

Using the Source Editor to Edit Beans

Understanding the IDE's Error Information

Compiling and Validating Enterprise Beans

Saving Your Changes

Renaming an Enterprise Bean

Modifying a Bean Based on Another Bean

Copying and Pasting an Enterprise Bean

Exchanging Bean Classes or Interfaces

Editing a Bean's Methods

Viewing a Method

Changing an Entity Bean's Fields

Renaming a Field

Changing the Type of a Field

Deleting an Enterprise Bean

B. Migrating and Upgrading EJB 1.1 Enterprise Beans

Understanding Updates in Recent Releases

Making Specific Changes

Converting a CMP 1.x Entity Bean

Avoiding the Use of New Features in an Old Bean

Don't Add Local Interfaces to a CMP 1.x Entity Bean

Don't Add Local EJB References, Either

Index