サンプル: アクセス Bean の使用

このサンプルでは、3 つの既存の CMP エンティティー Bean に対する EJB 1.1 アクセス Bean を示します。

アクセス Bean の作成方法を理解するために、単純なサンプルを考えます。 このサンプルでは、以下の名前を持つ 3 つの既存 CMP EJB のそれぞれに、 アクセス Bean を 1 つずつ作成する必要があります。

これらの EJB の CMP フィールドおよび関係、また、各 EJB に対して生成する必要があるアクセス Bean 型を、 次の表に示します。

EJB 名 CMP フィールド 関係 アクセス Bean 型 アクセス Bean 名
Employee id name salary Manager のスーパークラス。 Department と 1:N のアソシエーションになります (Employee には 1 つの部門があり、Department には多数の従業員がいます) rowset EmployeeAccessBean
Manager parkinglotnum Employee のサブクラス (Employee から継承) コピー・ヘルパー ManagerAccessBean
Department id name projectcode Employee と 1:N のアソシエーションになります (Department には複数の従業員がいて、Employee には 1 つの部門があります) コピー・ヘルパー DepartmentAccessBean

本トピックの以降の部分で、以下の 3 つのアクセス Bean に対する拡張サンプル・コードを紹介します。

このサンプル・コードには、EJB インターフェース、アクセス Bean メソッド・シグニチャー、 およびクライアント・プログラムが含まれています。

このサンプルの目的上、すべてのクラスが empexample という名前のパッケージに含まれ、 EJB が ABExample という名前の EJB グループに含まれていることを前提とします。 Department、Employee、および Manager アクセス Bean に関連付けされたサンプル・コードを、以下のセクションに示します。

EJB インターフェース

次のコード・サンプルに、EJB インターフェースが提供されています。

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 アクセス Bean メソッド・シグニチャー

サンプル・コードのこの時点では、3 つの CMP EJB が完了していることを前提としています。 Manager EJB は、Employee EJB から継承され、Employee EJB は、Department EJB と 1:N のアソシエーションになっています。

ここでアクセス Bean、すなわち、Department および Manager EJB のコピー・ヘルパー、 および Employee EJB の rowset を生成します。 すべての CMP フィールドは、getter における関連付けされた CMP EJB のキーを除き、 getter および setter のストリング・コンバーターを使用せずにキャッシュに入れられていることを想定しています。

次のコード・サンプルでは、アクセス Bean のメソッド・シグニチャーが提供されています。

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 クライアント・プログラム

次のコード・サンプルでは、EJB クライアント・プログラムの、 アクセス Bean に対するさまざまなオペレーションの実行を示しています。

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();
    }

}
}
関連概念
アクセス Bean
JSP ファイルおよびサーブレットのプログラミング・モデル (アクセス Bean)
EJB アクセス Bean およびクライアント・アプリケーション
関連タスク
EJB アクセス Bean の作成

フィードバック