挿入された Java コードは、EJB 参照を使用してエンタープライズ Bean のリモートまたはローカル・ホーム・インターフェースをルックアップして、 エンタープライズ Bean のインターフェースをルックアップする「find」メソッドを呼び出します。
このコードを素早く挿入するために「断片」ビューを使用すると、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 ハンドラー) と呼ばれる静的メソッドがあります。 このメソッドを使用すると、ホーム・インターフェースを検索するときに発生するエラー条件に対して、 特定のエラー・ハンドラーを指定できます。デフォルトのハンドラーは、処理される例外について printStackTrace() を呼び出すだけです。