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 local reference

To access an EJB’s local interface, instead of the remote interface, define an EJB local reference (ejb-local-ref). Local interfaces are available only to EJB components, Java servlets, and JSPs hosted on the same server as the target component. This sample declares a local reference to the Employee bean, which provides access to its local interface:

<ejb-local-ref>
   <ejb-ref-name>ejb/EmployeeLocal</ejb-ref-name>
   <ejb-ref-type>Entity</ejb-ref-type>
   <local-home>
      com.wooster.empl.EmployeeLocalHome
   </local-home>
   <local>com.wooster.empl.EmployeeLocal</local>
   <ejb-link>Employee</ejb-link>
</ejb-local-ref>

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 example creates a link to the Employee enterprise bean, by adding an ejb-link element to the bean’s EJB reference definition:

<ejb-ref>
   <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 EAServer Manager to add and configure EJB references in Web applications, EJB components, and application clients, see “EJB references” in Chapter 21, “Creating Web Applications,” in the EAServer Programmer’s Guide.