Java EJB Developing a CMP Bean

Developing a CMP Bean


This material is from the book JBuilder Developer’s Guide.

© Copyright Sams Publishing. All rights reserved.


It is now time to look at the process of building an entity bean. We will develop a simple CMP bean, but spend most of our time investigating and perfecting the process.

The process has some similarities to creating session beans. The following is the process we are going to use to investigate and build an entity bean:

  • Implement the home interface.

  • Define the appropriate finder methods.

  • Implement the component interface.

  • Define the bean implementation class.

  • Build the deployment descriptor.

  • Deploy the bean.

  • Test the bean.

Defining the Home Interface

The home interface for an entity bean is similar to the home interface of a session bean. To review, its purpose is to create an instance, find existing instances, or remove an instance; the same rules apply to an entity bean (see Listing 1).

Listing 1 Home Interface for Our Employee Entity Bean

import javax.ejb.*;
import java.util.*;

public interface EmployeeHome extends javax.ejb.EJBLocalHome {
 public Employee create(Short empNo) throws CreateException;
 public Employee findByPrimaryKey(Short empNo) throws FinderException;
}

The create() method is used to create an instance of a class based on this primary key. For our employee table, the empNo is the primary key, thus we will also use empNo as the primary key of our entity bean. The second method type is called a finder method. With entity beans, to create an instance we use the create() method, but finding an existing instance is just as important if not more important. With that requirement, we need finder methods to provide the capability to locate a specific instance or collection of instances of data.

The findByPrimaryKey() is a method that has an argument representing the primary key; in our case, it is empNo. This searches for an employee with the given primary key either returning a remote interface to the instance or through an exception.

Latest Posts

Related Stories