要了解如何创建访问 bean,考虑一个简单的 EJB 1.1 示例:其中您需要为具有下列名称的三个现有 CMP EJB 中的每一个创建访问 bean:
下表列示了这些 EJB 的 CMP 字段和关系以及需要为每个 EJB 生成的访问 bean 的类型:
EJB 名 | CMP 字段 | 关系 | 访问 bean 类型 | 访问 bean 名称 |
---|---|---|---|---|
Employee(职员) | 标识 姓名 薪水 | Manager(经理)的超类。参与与 Department(部门)的 1:N 关联(Employee(职员)具有一个部门,但 Department(部门)具有许多个职员) | 行集 | EmployeeAccessBean |
Manager(经理) | parkinglotnum(停车位编号) | Employee(职员)的子类(继承自 Employee) | 复制助手 | ManagerAccessBean |
Department(部门) | 标识 姓名 项目代码 | 参与与 Employee(职员)的 1:N 关联(Department(部门)具有许多职员,但 Employee(职员)只具有一个部门) | 复制助手 | DepartmentAccessBean |
在本主题的其余部分,显示了下列三个访问 bean 的大量样本代码:
样本代码包括 EJB 接口、访问 bean 方法特征符和客户机程序。
就此示例而言,假定所有的类都驻留在名为 empexample 的包中,而 EJB 驻留在名为 ABExample 的 EJB 组中。与 Department(部门)、Employee(职员)和 Manager(经理)访问 bean 相关联的样本代码可以在后面的下列各节中找到:
在以下代码样本中,提供了 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;}
在样本代码中的此处,假定三个 CMP EJB 都是完整的。“经理 EJB”继承自“职员 EJB”,而“职员 EJB”与“部门 EJB”之间具有 1:N 关联。
现在,将生成访问 bean:“部门 EJB”和“经理 EJB”的复制助手,以及“职员 EJB”的行集。可假定所有 CMP 字段都已存入高速缓存而无须对 getter 和 setter(相关联 CMP EJB 的键的 getter 除外)使用字符串转换器。
下列代码样本为访问 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 ) }
在以下代码样本中,显示了对访问 bean 执行各种操作的 EJB 客户机程序:
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(); } } }