EJB references

An EJB reference identifies the home of an enterprise Bean. You can use the deployment descriptor to create a link between an EJB reference and an enterprise Bean, contained within an EJB JAR file. Deployment descriptor interfaces allow an application component to access an enterprise Bean’s home interface using EJB references.

To locate an enterprise Bean’s home interface, declare an EJB reference in the deployment descriptor and use JNDI to look up the interface. The referenced enterprise Bean must be in the ejb subcontext of the application component’s environment.

Declaring an EJB reference

You can declare an EJB reference in the deployment descriptor using the ejb-ref element. The data between the opening and closing ejb-ref tags defines an ejb-ref element. This code sample defines an EJB reference to the Employee entity Bean:

<ejb-ref>
   <description>
      Reference to the Employee entity Bean
   </description>
   <ejb-ref-name>ejb/Employee</ejb-ref-name>
   <ejb-ref-type>Entity</ejb-ref-type>
   <home>com.wooster.empl.EmployeeHome</home>
   <remote>com.wooster.empl.Employee</remote>
</ejb-ref>

An ejb-ref element contains:

This code sample illustrates how to use JNDI to look up the home interface of the Employee enterprise Bean:

// Get the default initial JNDI context
Context initContext = new InitialContext();

// Look up the home interface of the Employee enterprise
// Bean
Object result =
   initContext.lookup(“java:comp/env/ejb/Employee”);

// Convert the result to the correct type
EmployeeHome empHome = (EmployeeHome)
   javax.rmi.PortableRemoteObject.narrow(result,
   EmployeeHome.class);

Declaring an EJB link

You can define a link from an EJB reference to an enterprise Bean by declaring an ejb-link element in the deployment descriptor. The application component and the target enterprise Bean must be in the same J2EE application. This sample code creates a link to the Employee enterprise Bean, by adding an ejb-link element to the Bean’s EJB reference definition:

<ejb-ref>
   <description>
      Reference to the Employee entity Bean
   </description>
   <ejb-ref-name>ejb/Employee</ejb-ref-name>
   <ejb-ref-type>Entity</ejb-ref-type>
   <home>com.wooster.empl.EmployeeHome</home>
   <remote>com.wooster.empl.Employee</remote>
   <ejb-link>Employee</ejb-link>
</ejb-ref>

For information about using the Adaptive Server plug-in to add and configure EJB references in EJB components, see Chapter 6, “Working with EJB Packages and Components.”