Skip Headers
Oracle® Containers for J2EE Enterprise JavaBeans Developer's Guide
10g (10.1.3.5.0)

Part Number E13981-01
Go to Documentation Home
Home
Go to Book List
Book List
Go to Table of Contents
Contents
Go to Index
Index
Go to Feedback page
Contact Us

Go to previous page
Previous
Go to next page
Next
View PDF

Accessing an EJB 3.0 Enterprise Bean

You can directly look up a bean instance from JNDI (or use resource injection in an EJB 3.0 EJB client) and retrieve a bean instance without the home interface. If the <home> or <local-home> element is removed from an EJB reference, a bean instance is returned from JNDI instead of the home.

The bean instance is created by executing the no-argument create method on the home interface. Stateful session beans and entity beans can also use this shortcut, but they must have a no-argument create method, or otherwise an exception will be thrown at lookup time.

In both cases, the syntax used in obtaining the reference to the EJB business interface is independent of whether the business interface is local or remote. In the case of remote access, the actual location of a referenced enterprise bean and EJB container are, in general, transparent to the client using the remote business interface of the bean.

Using EJB 3.0, you can look up an enterprise bean using resource injection (see "Using Annotations") or the InitialContext (see "Using Initial Context").

Alternatively, you can define an environment reference to an EJB 3.0 bean using OC4J-proprietary annotations or deployment XML (see "EJB Environment References").

Using Annotations

Example 29-3 shows how to use annotations and dependency injection to access an EJB 3.0 enterprise bean from an EJB client.

Example 29-3 Injecting an EJB 3.0 Enterprise Bean in an EJB 3.0 EJB Client

@EJB AdminService bean;
 
    public void privilegedTask() {
        bean.adminTask();
    }

Using Initial Context

This section describes the following:

For more information, see "Configuring the Initial Context Factory".

Looking Up the Remote Interface of an EJB 3.0 Enterprise Bean Using ejb-ref

To look up the remote interface of an enterprise bean using an ejb-ref, do the following:

  1. Define an ejb-ref element for the enterprise bean in the ejb-jar.xml file.

    Example 29-4 ejb-jar.xml For an ejb-ref Element

    <ejb-ref>
        <ejb-ref-name>ejb/Test</ejb-ref-name>
        <ejb-ref-type>Session</ejb-ref-type>
        <local>Test</local>
    </ejb-ref>
    

    For more information, see "Configuring an Environment Reference to a Remote EJB: Clustered or Combined Web Tier and EJB Tier").

  2. Determine whether or not a prefix is required (see "Selecting an EJB Reference").

  3. Look up the enterprise bean using the ejb-ref-name element and the appropriate prefix (if required).

    Example 29-5 Looking Up Using ejb-ref in an EJB 3.0 EJB Client Using Initial Context

    InitialContext ic = new InitialContext();
    Cart cart = (Cart)ic.lookup("java:comp/env/ejb/Test");
    

    For more information, see "Configuring the Initial Context Factory".

Looking Up the Remote Interface of an EJB 3.0 Enterprise Bean Using location

To look up the remote interface of an EJB using its location, do the following:

  1. Define the location attribute for a entity-deployment element in the orion-ejb-jar.xml file.

    Example 29-6 orion-ejb-jar.xml for location Attribute

    <entity-deployment
        name="Test"
        location="app/Test"
        ...
    >
    ...
    </entity-deployment>
    

    The default value for location attribute is the value of entity-deployment attribute name.

  2. Determine whether or not a prefix is required (see "Selecting an EJB Reference").

  3. Look up the enterprise bean using the location.

    Example 29-7 Looking Up Using location in an EJB 3.0 EJB Client Using Initial Context

    InitialContext ic = new InitialContext();
    Cart cart = (Cart)ic.lookup("java:comp/env/app/Test");
    

    For more information, see "Configuring the Initial Context Factory".

Looking up the Local Interface of an EJB 3.0 Enterprise Bean Using local-ref

To look up the remote interface of an EJB using an ejb-local-ref, do the following:

  1. Define an ejb-local-ref element for the enterprise bean in the ejb-jar.xml file.

    Example 29-8 ejb-jar.xml For an ejb-local-ref Element

    <ejb-local-ref>
        <ejb-ref-name>ejb/Test</ejb-ref-name>
        <ejb-ref-type>Session</ejb-ref-type>
        <local>Test</local>
    </ejb-local-ref>
    

    For more information, see "Configuring an Environment Reference to a Local EJB").

  2. Determine whether or not a prefix is required (see "Selecting an EJB Reference").

  3. Look up the enterprise bean using the ejb-ref-name and the appropriate prefix (if required).

    Example 29-9 Looking Up Using local-ref in an EJB 3.0 EJB Client Using Initial Context

    InitialContext ic = new InitialContext();
    Cart cart = (Cart)ctx.lookup("java:comp/env/ejb/Test");
    

    For more information, see "Configuring the Initial Context Factory".

Looking up the Local Interface of an EJB 3.0 Enterprise Bean Using local-location

To look up the local interface of an EJB using its local-location, do the following:

  1. Define the local-location attribute of the entity-deployment element in the orion-ejb-jar.xml file.

    Example 29-10 orion-ejb-jar.xml for local-location Attribute

    <entity-deployment
        name="Test"
        local-location="app/Test"
        ...
    >
    ...
    </entity-deployment>
    

    The default value for local-location is the value of entity-deployment attribute name.

  2. Determine whether or not a prefix is required (see "Selecting an EJB Reference").

  3. Look up the enterprise bean using the local-location.

    Example 29-11 Looking Up Using local-location in an EJB 3.0 EJB Client Using Initial Context

    InitialContext ic = new InitialContext();
    Cart cart = (Cart)ctx.lookup("java:comp/env/app/Test");
    

    For more information, see "Configuring the Initial Context Factory".