The inserted Java code uses an EJB reference to look up the remote or local home interface of an enterprise bean and calls the "find" method to find an instance of the enterprise bean.
By using the Snippets view to quickly insert this code, you can avoid manually coding the appropriate business logic to find an instance of a bean. The Java code is generated and inserted in the current spot of the Java file that you are editing.
Example snippet insertion for calling an EJB find method
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; } }
To insert Java code for finding an instance of an enterprise bean:
The ServiceLocatorManager class has a static method called setErrorHandler(ServiceLocatorErrorHandler handler) that you can use to specify a specific error handler for error conditions that occur when looking up the home interface. The default handler simply calls printStackTrace() on the exception that is handled.