Example: Using access beans

This example shows an EJB 1.1 access bean for three existing CMP entity beans.

To understand how to create access beans, consider a simple EJB 1.1 example in which you need to create an access bean for each of three existing CMP EJBs that have the following names:

The CMP fields and relationships for these EJBs are shown in the following table, as well as the type of access bean that needs to be generated for each EJB:

EJB name CMP fields Relationships Access bean type Access bean name
Employee id name salary Superclass of Manager. Participates in 1:N association with Department (Employee has one department, but Department has many employees) Rowset EmployeeAccessBean
Manager parkinglotnum Subclass of Employee (inherits from Employee) Copy helper ManagerAccessBean
Department id name projectcode Participates in 1:N association with Employee (Department has many employees, but Employee has one department) Copy helper DepartmentAccessBean

In the remainder of this topic, extensive sample code is shown for the following three access beans:

The sample code includes the EJB interfaces, access bean method signatures, and client program.

For the purposes of this example, assume that all of the classes reside in a package named empexample and that the EJBs reside in an EJB group named ABExample. The sample code associated with the Department, Employee, and Manager access beans is found in the following sections below:

EJB Interfaces

In the following code sample, the EJB interfaces are provided:

public interface Employee extends javax.ejb.EJBObject {
  empexample.Department getDepartment() throws java.rmi.RemoteException,
     javax.ejb.FinderException;
  empexample.DepartmentKey getDepartmentKey() throws java.rmi.RemoteException;
  java.lang.String getName() throws java.rmi.RemoteException;
  float getSalary() throws java.rmi.RemoteException;
  void privateSetDepartmentKey(empexample.DepartmentKey inKey) throws
     java.rmi.RemoteException;
  void secondarySetDepartment(empexample.Department aDepartment) throws
     java.rmi.RemoteException;
  void setDepartment(empexample.Department aDepartment) throws
     java.rmi.RemoteException;
  void setName(java.lang.String newValue) throws java.rmi.RemoteException;
  void setSalary(float newValue) throws java.rmi.RemoteException;
}
public interface EmployeeHome extends javax.ejb.EJBHome {
  empexample.Employee create(int argId) throws javax.ejb.CreateException,
     java.rmi.RemoteException;
  empexample.Employee create(int argId, int depId) throws
     javax.ejb.CreateException, java.rmi.RemoteException;
  empexample.Employee findByPrimaryKey(empexample.EmployeeKey key) throws
     java.rmi.RemoteException, javax.ejb.FinderException;
  java.util.Enumeration findEmployeeByDepartment(empexample.DepartmentKey inKey) throws
     java.rmi.RemoteException, javax.ejb.FinderException;
}
public interface Manager extends Employee {
  int getParkinglotnum() throws java.rmi.RemoteException;
  void setParkinglotnum(int newValue) throws java.rmi.RemoteException;
}
public interface ManagerHome extends javax.ejb.EJBHome {
  empexample.Manager create(int argId) throws javax.ejb.CreateException,
     java.rmi.RemoteException;
  empexample.Manager findByPrimaryKey(EmployeeKey key) throws
     java.rmi.RemoteException, javax.ejb.FinderException;
}
public interface Department extends javax.ejb.EJBObject {
  void addEmployee(empexample.Employee anEmployee) throws
     java.rmi.RemoteException;
  java.util.Enumeration getEmployee() throws java.rmi.RemoteException,
     javax.ejb.FinderException;
  java.lang.String getName() throws java.rmi.RemoteException;
  int getProjectcode() throws java.rmi.RemoteException;
  void secondaryAddEmployee(empexample.Employee anEmployee) throws
     java.rmi.RemoteException;
  void secondaryRemoveEmployee(empexample.Employee anEmployee) throws
      java.rmi.RemoteException;
  void setName(java.lang.String newValue) throws java.rmi.RemoteException;
  void setProjectcode(int newValue) throws java.rmi.RemoteException;
}
public interface DepartmentHome extends javax.ejb.EJBHome {
  empexample.Department create(int argId) throws javax.ejb.CreateException,
     java.rmi.RemoteException;
  empexample.Department findByPrimaryKey(empexample.DepartmentKey key) throws
     java.rmi.RemoteException, javax.ejb.FinderException;
}

EJB Access Bean Method Signatures

At this point in the sample code, assume that the three CMP EJBs are complete. The Manager EJB inherits from the Employee EJB, and the Employee EJB has a 1:N association with the Department EJB.

Now you would generate your access beans: a copy helper for the Department and Manager EJBs, and a rowset for the Employee EJB. You can assume that all CMP fields are cached without using string converters for the getters and setters, with the exception of the getters for the key of the associated CMP EJBs.

The following code sample provides the method signatures for the access beans:

public class EmployeeAccessBean extends com.ibm.ivj.ejb.runtime.AbstractEntityAccessBean
   implements EmployeeAccessBeanData {
  /**
   * Zero argument constructor used to initialize the access bean.
   *
   * This constructor corresponds to the following home interface method:
   *
   * public abstract empexample.Employee empexample.EmployeeHome.create(int) throws
   *    javax.ejb.CreateException,java.rmi.RemoteException
   *
   * The home interface method properties need to be set by calling
   * the following setter methods before calling any business methods:
   * setInit_argId( int )
   * setInit_depId( int )
   */
   public EmployeeAccessBean()
   public EmployeeAccessBean(empexample.EmployeeKey arg0) throws
      java.rmi.RemoteException, javax.ejb.FinderException,
      javax.naming.NamingException
   public EmployeeAccessBean ( javax.ejb.EJBObject o ) throws
      java.rmi.RemoteException
   public EmployeeAccessBean ( int arg0 ) throws javax.ejb.CreateException,
      java.rmi.RemoteException, javax.naming.NamingException 
   public void commitCopyHelper() throws java.rmi.RemoteException,
      javax.ejb.CreateException, javax.ejb.FinderException, 
      javax.naming.NamingException
   protected String defaultJNDIName()
   private empexample.EmployeeHome ejbHome() throws
      java.rmi.RemoteException, javax.naming.NamingException
   private empexample.Employee ejbRef() throws java.rmi.RemoteException
   public java.util.Enumeration findEmployeeByDepartment(empexample.DepartmentKey arg0)
      throws java.rmi.RemoteException, javax.ejb.FinderException, 
      javax.naming.NamingException
   public empexample.DepartmentAccessBean getDepartment() throws
      java.rmi.RemoteException, javax.ejb.FinderException,
      javax.ejb.CreateException, javax.naming.NamingException
   public empexample.DepartmentKey getDepartmentKey() throws
      java.rmi.RemoteException, javax.ejb.CreateException,
      javax.naming.NamingException
   public java.lang.String getName() throws java.rmi.RemoteException,
      javax.ejb.CreateException, javax.ejb.FinderException,
      javax.naming.NamingException
   public float getSalary() throws java.rmi.RemoteException,
      javax.ejb.CreateException, javax.ejb.FinderException,
      javax.naming.NamingException
   protected void instantiateEJB() throws javax.ejb.CreateException,
      java.rmi.RemoteException, javax.naming.NamingException
   protected boolean instantiateEJBByPrimaryKey() throws
      java.rmi.RemoteException, javax.ejb.CreateException,
      javax.naming.NamingException
   public void privateSetDepartmentKey(empexample.DepartmentKey arg0) throws
      java.rmi.RemoteException, javax.ejb.CreateException,
      javax.naming.NamingException
   public void refreshCopyHelper() throws java.rmi.RemoteException,
      javax.ejb.CreateException, javax.ejb.FinderException,
      javax.naming.NamingException
   public void secondarySetDepartment(empexample.Department arg0) throws
      java.rmi.RemoteException, javax.ejb.CreateException,
      javax.naming.NamingException
   public void setDepartment(empexample.Department arg0) throws
      java.rmi.RemoteException, javax.ejb.CreateException,
      javax.naming.NamingException
   public void setInit_argId( int newValue )
   public void setInit_depId( int newValue )
   public void setName( java.lang.String newValue )
   public void setSalary( float newValue )
}
public class ManagerAccessBean extends
   com.ibm.ivj.ejb.runtime.AbstractEntityAccessBean implements ManagerAccessBeanData {
  /**
   * Zero argument constructor used to initialize the access bean.
   *
   * This constructor corresponds to the following home interface method:
   *
   * public abstract empexample.Manager empexample.ManagerHome.create(int) throws
   *    javax.ejb.CreateException, java.rmi.RemoteException
   *
   * The home interface method properties need to be set by calling
   * the following setter methods before calling any business methods:
   * setInit_argId( int )
   */
   public ManagerAccessBean ()
   public ManagerAccessBean(empexample.EmployeeKey arg0) throws
      java.rmi.RemoteException, javax.ejb.FinderException,
      javax.naming.NamingException
   public ManagerAccessBean ( javax.ejb.EJBObject o ) throws
      java.rmi.RemoteException
   public void commitCopyHelper() throws java.rmi.RemoteException,
      javax.ejb.CreateException, javax.ejb.FinderException,
      javax.naming.NamingException
   protected String defaultJNDIName()
   private empexample.ManagerHome ejbHome() throws java.rmi.RemoteException,
      javax.naming.NamingException
   private empexample.Manager ejbRef() throws java.rmi.RemoteException
   public empexample.DepartmentAccessBean getDepartment() throws
      java.rmi.RemoteException, javax.ejb.FinderException, 
      javax.ejb.CreateException, javax.naming.NamingException
   public empexample.DepartmentKey getDepartmentKey() throws 
      java.rmi.RemoteException, javax.ejb.CreateException, 
      javax.naming.NamingException
   public java.lang.String getName() throws java.rmi.RemoteException, 
      javax.ejb.CreateException, javax.ejb.FinderException, 
      javax.naming.NamingException
   public int getParkinglotnum() throws java.rmi.RemoteException, 
      javax.ejb.CreateException, javax.ejb.FinderException, 
      javax.naming.NamingException
   public float getSalary() throws java.rmi.RemoteException, 
      javax.ejb.CreateException, javax.ejb.FinderException, 
      javax.naming.NamingException 
   protected void instantiateEJB() throws javax.ejb.CreateException,
      java.rmi.RemoteException, javax.naming.NamingException
   protected boolean instantiateEJBByPrimaryKey() throws
      java.rmi.RemoteException, javax.ejb.CreateException,
      javax.naming.NamingException
   public void privateSetDepartmentKey(empexample.DepartmentKey arg0) throws
      java.rmi.RemoteException, javax.ejb.CreateException,
      javax.naming.NamingException
   public void refreshCopyHelper() throws java.rmi.RemoteException,
      javax.ejb.CreateException, javax.ejb.FinderException, 
      javax.naming.NamingException
   public void secondarySetDepartment(empexample.Department arg0) throws
      java.rmi.RemoteException, javax.ejb.CreateException, 
      javax.naming.NamingException
   public void setDepartment(empexample.Department arg0) throws
      java.rmi.RemoteException, javax.ejb.CreateException, 
      javax.naming.NamingException
   public void setInit_argId( int newValue )
   public void setName( java.lang.String newValue )
   public void setParkinglotnum( int newValue )
   public void setSalary( float newValue )
}
public class DepartmentAccessBean 
   extends com.ibm.ivj.ejb.runtime.AbstractEntityAccessBean
   implements DepartmentAccessBeanData {
  /**
   * Zero argument constructor used to initialize the access bean.
   *
   * This constructor corresponds to the following home interface method:
   *
   * public abstract empexample.Department empexample.DepartmentHome.create(int)
   *    throws javax.ejb.CreateException,java.rmi.RemoteException
   *
   * The home interface method properties need to be set by calling
   * the following setter methods before calling any business methods:
   * setInit_argId( int )
   */
   public DepartmentAccessBean ()
   public DepartmentAccessBean(empexample.DepartmentKey arg0) throws
      java.rmi.RemoteException, javax.ejb.FinderException, 
      javax.naming.NamingException
   public DepartmentAccessBean ( javax.ejb.EJBObject o ) throws 
      java.rmi.RemoteException
   public void addEmployee(empexample.Employee arg0) throws
      java.rmi.RemoteException, javax.ejb.CreateException,
      javax.naming.NamingException
   public void commitCopyHelper() throws java.rmi.RemoteException,
      javax.ejb.CreateException, javax.ejb.FinderException,
      javax.naming.NamingException
   protected String defaultJNDIName()
   private empexample.DepartmentHome ejbHome() throws
      java.rmi.RemoteException, javax.naming.NamingException
   private empexample.Department ejbRef() throws
      java.rmi.RemoteException
   public java.util.Enumeration getEmployee() throws java.rmi.RemoteException,
      javax.ejb.FinderException, javax.ejb.CreateException, 
      javax.naming.NamingException
   public java.lang.String getName() throws java.rmi.RemoteException,
      javax.ejb.CreateException, javax.ejb.FinderException, 
      javax.naming.NamingException
   public int getProjectcode() throws java.rmi.RemoteException, 
      javax.ejb.CreateException, javax.ejb.FinderException, 
      javax.naming.NamingException
   protected void instantiateEJB() throws javax.ejb.CreateException, 
      java.rmi.RemoteException, javax.naming.NamingException
   protected boolean instantiateEJBByPrimaryKey() throws 
       java.rmi.RemoteException, javax.ejb.CreateException, 
       javax.naming.NamingException
   public void refreshCopyHelper() throws java.rmi.RemoteException, 
       javax.ejb.CreateException, javax.ejb.FinderException,
       javax.naming.NamingException
   public void secondaryAddEmployee(empexample.Employee arg0) throws
       java.rmi.RemoteException, javax.ejb.CreateException, 
       javax.naming.NamingException
   public void secondaryRemoveEmployee(empexample.Employee arg0) throws
       java.rmi.RemoteException, javax.ejb.CreateException, 
       javax.naming.NamingException
   public void setInit_argId( int newValue ) 
   public void setName( java.lang.String newValue )
   public void setProjectcode( int newValue )
}

EJB Client Program

In the following code sample, an EJB client program is shown performing various operations on the access beans:

package empexample;

import javax.ejb.*;
import com.ibm.ivj.ejb.runtime.*;

public class EmpDepTest {

/**
 * Simple test of the Employee, Department, and Manager
 * access beans.
 *
 * @param args an array of command-line arguments
 * args[0] = Employee ID#
 * args[1] = Employee Name
 * args[2] = Employee Salary
 * args[3] = Department ID#
 * args[4] = Parking lot# (optional)
 */

public static void main(java.lang.String[] args) {

    EmployeeAccessBean empab = null;
    ManagerAccessBean mgrab = null;
    DepartmentAccessBean depab = null;
    int empid;
    String empname;
    float empsalary;
    int mgrparknum;
    int depid;

    try {
        empid = Integer.parseInt(args[0]);
        empname = args[1];
        empsalary = Float.parseFloat(args[2]);
        depid = Integer.parseInt(args[3]);
        mgrparknum = Integer.parseInt(args[4]);

        // Attempt to create a new employee with given info
        try {
            empab = new EmployeeAccessBean();
            empab.setInit_argId(empid);
            empab.setInit_depId(depid);

            

            // Set the various attributes (gets written to cache)
            empab.setName(empname);
            empab.setSalary(empsalary);

            // Flush the cache to the server
            empab.commitCopyHelper();
        }

        // If duplicate key exception occurs, find the pre-existing
        // instance instead
        catch ( DuplicateKeyException dke ) {
                empab = new EmployeeAccessBean(new EmployeeKey(empid));

                // Fill the cache with all the attributes
                empab.refreshCopyHelper();

                // Update the pre-existing Employee with new info
                // and update the entity bean by flushing the cache
                empab.setName(empname); 
                empab.setSalary(empsalary);
                empab.commitCopyHelper();
        }

        // Display employee info
        // Get the Employee bean's key (assume key class has
        // getters for key fields)
        EmployeeKey empkey = (EmployeeKey) empab.__getKey();
        System.out.println("Employee ID#: " + empkey.getId());
        System.out.println("Employee Name: " + empab.getName());
        System.out.println("Employee Salary: " + empab.getSalary());
        
        // Get the Department access bean (for associated EJB) for
        // this Employee
        depab = empab.getDepartment();
        if ( depab != null ) {
            DepartmentKey depkey = (DepartmentKey) depab.__getKey();

            // Find all the employees in this department
            // This is also shows the use of the new AccessBeanEnumeration
            // class which is a special enumeration class that only
            // instantiates the EJB object when nextElement() is called.
            // All access bean finder methods returning Enumerations of 
            // EJBObject's now return this special enumeration class
            System.out.println("\nFinding all employees for department "  
                                                  + depab.getName());
            AccessBeanEnumeration aem =
                (AccessBeanEnumeration) empab.findEmployeeByDepartment(depkey);

            // Use an access bean table (rowset) to organize and manipulate the
            // enumeration of Employee access beans.
            // Usually, a session bean would first create this table before
            // passing it on to a JSP where the enumeration of access beans
            // can be handled like a rowset using indexes.
            // Rows (or EJB instances) can then be added or removed from the
            // rowset.
            EmployeeAccessBeanTable emptable = new EmployeeAccessBeanTable();

            // One possible way of filling the table is to call the method below
            //emptable.setEmployeeAccessBean(aem);

            // This is another way
            while ( aem.hasMoreElements() ) {
                  EmployeeAccessBean empab_temp = (EmployeeAccessBean) aem.nextElement();
                  emptable.addRow(empab_temp);
                  empab_temp.refreshCopyHelper();
                  System.out.println("    Employee Name: " + empab_temp.getName());
                  System.out.println("    Employee Salary: " + empab_temp.getSalary());
            }

            // Once the table is built, the client can go about working with
            // with it and performing various operations on it without any
            // server-side communications

        }

        else {
             System.out.println("Could not find a department for employee id#" + empid);
        }

    }


    catch ( Exception e ) {
            e.printStackTrace();
    }

}
}

 

Parent topic

Creating EJB access beans

 

Related concepts

Access beans
Programming model for JSP files and servlets (access beans)
EJB access beans and client applications

 

Related tasks

Creating EJB access beans