Adding EJB references

An EJB reference is a logical name used by a client (or another bean) to locate the home interface of an enterprise bean. You can define references to enterprise beans in EJB modules, J2EE application client modules, and J2EE dynamic Web modules.

It is a best practice to use an EJB reference for any enterprise bean that you need to reference. Using an EJB reference will allow you to safely write Java code to lookup the home interface of the target enterprise bean without the worry of the binding changing for the target enterprise bean. This is necessary if you need to install the same EJB module on the same server with different bindings.

At deployment, the EJB reference is bound to the enterprise bean's home in the target operational environment. The container makes the application's EJB references available in a JNDI naming context.

Note:

For each EJB reference that you define, an ejb-ref element is added to the deployment descriptor.

To define an EJB reference:

  1. Open the Add Reference wizard from the deployment descriptor editor for your J2EE module.

  2. Select EJB reference and click Next.

  3. Choose one of the following options for specifying the enterprise bean that you want to reference (the referenced bean):

    • Enterprise beans in the workspace: Select this option to choose an enterprise bean from the projects currently in your workspace. The tree shows EJB modules in the workspace and lists the available enterprise beans in the EJB modules.

    • Enterprise beans not in the workspace: Select this option to choose an enterprise bean outside of your workspace.

  4. If you selected Enterprise beans in the workspace complete the following steps:

    1. Expand the project tree and select the enterprise bean that you want to reference.

    2. In the Name field, enter a name for the reference, or accept the default provided by the wizard (recommended).

    3. If you are referencing an EJB 2.1 bean, in the Ref type field specify whether you are referencing the local or remote interface of the enterprise bean.
      Remember:

      • EJB 1.1 enterprise beans and application client modules cannot reference the local interface of an enterprise bean. They must reference the remote interface.

      • In order for a module or a bean to reference the local interface of an enterprise bean, the referenced bean must be included in the same enterprise application (EAR) as the module or bean that is referencing it.

      Important: If you reference an enterprise bean in another EAR, and that bean's project does not include an EJB client JAR, the workbench automatically creates an EJB client JAR for that referenced EJB project. This improves runtime performance and allows for visibility across different EAR files.

  5. If you selected Enterprise beans not in the workspace, complete the following steps:

    1. In the Name field, enter a name for the reference.

    2. In the Type field, specify whether the referenced bean is a session or entity bean.

    3. In the Home field, enter the qualified path to the remote home of the enterprise bean. Click Browse to locate the remote home using the Type Selection dialog box.

    4. In the Remote field, enter the qualified path to the remote interface for the enterprise bean. Click Browse to locate the remote interface using the Type Selection dialog box.

  6. Click Next to review your selections and enter a description for the reference.

  7. Click Finish.

 

Related concepts

Client Deployment Descriptor editor

 

Related tasks

Defining references in J2EE modules
Adding message destination references
Adding resource manager connection factory references
Adding resource environment references
Adding security role references
Adding Web service references