Create a Simple EJB Session Bean

Create a Simple EJB Session Bean

You can quickly create a project in which to organize your application files. Then work in JDeveloper's IDE to develop and test run your EJB session bean.

Create a New Application: Use the Create Application wizard to create a generic application.

Create a Session Bean: Use the Create Session Bean wizard to create a new EJB 3.0 session bean.

Add a Business Method: Use the Java source editor to add a method to the bean class.

Create a Java Client: Use a utility to create a sample Java client. Then use the Java source editor to add code to the client to call the business method.

Run the Session Bean, then Run the Java Client: Test the Java client in the IDE's runtime service.

 

Create a New Application and Project

tell me more icon Creating a New Application and Project

The JDeveloper application is the highest level in the organizational structure. While you are developing your application, it stores information about the objects you are working with. At the same time, it keeps track of your projects and all environment settings.  [tell me more...]

  1. Open the Create Application wizard [File > New > General > Applications] .
  2. To follow along with the example, enter the application name as SessionEJB.
  3. Select Generic Application in the Application Template list, and click Finish.

    tell me more icon Application Templates

    Application templates provide you with a quick way to create the project structure for standard applications with the appropriate combination of technologies already specified. The new application created from the template appears in the Application Navigator already partitioned into tiered projects, with the associated technology scopes set in each project.   [tell me more...]


    tell me more icon In the IDE

    The Application Overview window opens by default in the editor window area when you create a new application. All objects that you create within JDeveloper appear in the Application Overview, arranged by object type. You can create new objects, read about new types of objects that you haven't yet created, and initiate actions from the objects' context menus.   [tell me more...]

 

Create an EJB Session Bean

tell me more icon Creating an EJB Session Bean

In JDeveloper's IDE, you can quickly create a runnable session bean, and create business methods declaratively.  [tell me more...]

  1. In the Application Navigator, select the project you just created and launch the Create Session Bean wizard [File > New > Business Tier > EJB > Session Bean] .
  2. On step 1 of the wizard, Select EJB Version, select Enterprise JavaBeans 3.0 (Java EE 5.0), and click Next.
  3. On step 2, EJB Name and Options, enter HelloWorldSession as the EJB name to follow along with the example. Make sure Stateless and Container are selected. Confirm that Generate Session Facade Methods is not selected. Click Next.

    tell me more icon EJB Name and Options

    To follow along with the example, in this step and throughout the cards, enter values as shown in the instructions. Then you will be able to follow the steps in the cue cards exactly as written.   [tell me more...]

  4. On step 3, Class Definitions, enter acme.ejb.session.HelloWorldSessionBean as the bean class name, and click Next.

    tell me more icon EJB Class Definitions

    Changing the package name in the Bean Class field will put all the session bean information in its own package. Partitioning your EJBs in different packages can be less confusing when there are many components in a project.   [tell me more...]

  5. On step 4, EJB Home and Component Interfaces, select Implement a Remote Interface and deselect Implement a Local Interface. Click Finish.

    tell me more icon Local and Remote Interfaces

    When you create a session bean using the wizard, you have the option of creating local or remote interfaces, or both. Which interfaces you choose to generate depends on the type of client. If the client is running in the same virtual machine (VM), a local interface is the best choice. If the client is running a separate VM, a remote interface is required. Web clients run in the same VM, while Java clients run in a different VM. In the example, you will be creating a Java client, so you only need to generate the remote interface.  [tell me more...]

  6. Click the save all icon Save All to save your work.

    tell me more icon In the IDE

    When you complete the steps for creating your session bean, the Application Navigator and Structure window should look similar to these:  [tell me more...]

 

Add a Bean Method

tell me more icon Adding a Bean Method

A session bean contains business methods that can be executed by a client. In the example, you will use the Java source editor to add a method to the bean class generated by JDeveloper.  [tell me more...]

  1. In the Application Navigator double-click ejbsession icon HelloWorldSessionBean.java to open the bean class file in the Java source editor. The bean class file is located in the package icon acme.ejb.session package in the Application Sources folder.
  2. Add code to create a "Hello world" method.
    code sample icon Use sample code

    tell me more icon Bean Methods

    While you can add a new method using the Java source editor, you can also use JDeveloper's dialogs to create methods declaratively. Once you've selected a session bean in the Application Navigator, you can create a new method by right-clicking HelloWorldSessionBean in the Structure window, and then choosing EJB then choose New Method.   [tell me more...]

  3. In the Structure window for HelloWorldSessionBean.java, right-click method icon sayHello(String) and choose EJB then choose Properties.

    tell me more icon Bean Method Details

    The Bean Method Details dialog can be used when you edit an existing EJB method or create a new one.   [tell me more...]

  4. In the Bean Method Details dialog, select Expose through Remote interface, and click OK.
  5. Click the save all icon Save All to save your work.

    tell me more icon In the IDE

    When you complete the steps for adding a bean method and exposing the method through a remote interface, the Java source editor should look similar to this:   [tell me more...]

 

Create and Run a Java Client

tell me more icon Creating and Running a Java Client

JDeveloper includes a sample Java client utility for testing your business services. To begin generating a sample Java client, in the Application Navigator right-click a session bean and choose New Sample Java Client.  [tell me more...]

  1. In the Application Navigator, right-click ejbsession icon HelloWorldSessionBean.java and choose New Sample Java Client.
  2. In the Create Sample Java Client dialog, enter acme.client.HelloWorldSessionClient in the Client Class Name field to create the client in a separate package in the project. Make sure IntegratedWLSConnection is selected in the Application Server Connection dropdown list, then click OK.

    tell me more icon Creating a Sample Java Client

    When you create a sample Java client, you can choose to create the client in the same package as the session bean, or specify a different package. Putting your client files in a separate package is a good practice because it simplifies navigation in large projects.  [tell me more...]

  3. In the Java source editor for HelloWorldSessionClient.java, add code that calls the method you created previously.
    code sample icon Use sample code
  4. Rebuild the project by choosing Build then choose Rebuild <project_name>.
  5. In the Application Navigator, start the server by right-clicking ejbsession icon HelloWorldSessionBean.java and choosing Run.
  6. When the server has finished initializing, in the Application Navigator right-click javanode icon HelloWorldSessionClient.java and choose Run.

    tell me more icon In the IDE

    When you complete the steps for creating a sample Java client, the Application Navigator should look like this:   [tell me more...]

 

 

 

 

E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy