插入的 Java 代码使用 EJB 引用查找企业 bean 的远程或本地 home 接口,并调用“find”方法来查找企业 bean 的实例。
通过使用“片段”视图来快速插入此代码,可以避免手工对正确的业务逻辑进行编码以查找 bean 的实例。会生成 Java 代码并插入到正在编辑的 Java 文件的当前特别点。
插入片段以调用 EJB find 方法的示例
public class Test { public void findAccountsPayableByPrimaryKey() { // insert snippet here } }
import sample.Accountspayable; import sample.AccountspayableKey; import com.ibm.etools.service.locator.ServiceLocatorManager; import java.rmi.RemoteException; import sample.AccountspayableHome; import javax.ejb.FinderException; public class Test { private final static String STATIC_AccountspayableHome_REF_NAME = "ejb/Accountspayable"; private final static Class STATIC_AccountspayableHome_CLASS = AccountspayableHome.class; public void findAccountsPayableByPrimaryKey() { // insert snippet here Accountspayable anAccountspayable = find_AccountspayableHome_findByPrimaryKey(primaryKey); } protected Accountspayable find_AccountspayableHome_findByPrimaryKey( AccountspayableKey primaryKey) { AccountspayableHome anAccountspayableHome = (AccountspayableHome) ServiceLocatorManager .getRemoteHome(STATIC_AccountspayableHome_REF_NAME, STATIC_AccountspayableHome_CLASS); try { if (anAccountspayableHome != null) return anAccountspayableHome.findByPrimaryKey(primaryKey); } catch (FinderException fe) { // TODO Auto-generated catch block fe.printStackTrace(); } catch (RemoteException re) { // TODO Auto-generated catch block re.printStackTrace(); } return null; } }
要插入用于查找企业 bean 的实例的 Java 代码:
ServiceLocatorManager 类具有称为 setErrorHandler(ServiceLocatorErrorHandler handler) 的静态方法,可以使用该方法来为在查找 home 接口时出现的错误状态指定特定错误处理程序。缺省处理程序只对处理的异常调用 printStackTrace()。