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 an EJB Session Bean: Use the Create Session Bean wizard to create a new EJB 3.0 session bean.

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

Create and Run a Java Client: Create a sample Java client and add code to the client to call the business method. Run the session bean, then run 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 Applica tion 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

    When you complete the steps for creating a new application and project, the Projects panel in the Application Navigator should look like this:  [ 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 State less 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 Imp lement 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 should look similar to this:  [ 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 source editor to add a method to the bean class generated by JDeveloper.  [ tell me more...]





  1. If not already open, in the Application Navigator double-click ejbsession icon HelloWorldSessionBean.java to open the bean class file in the source editor.
  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 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

    You can use the Bean Method Details dialog to edit the details of an existing EJB method or create a new method.   [ tell me more...]



  4. In the Bean Method Details dialog, select Expose th rough 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 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 the 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 Clie nt Class Name field to create the client in a separate package in the project. Make sure IntegratedWebLogicServer is selected in the Appli cation 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 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 Re build < project_name> from the main menu. You should see a Successful compilation message in the Messages Log window.
  5. In the Application Navigator, start the integrated server by right-clicking ejbsession icon HelloWorldSessionBean.java and choosing R un .
  6. When the application has been deployed to the integrated server, in the Application Navigator right-click javanode icon HelloWorldSessionClient.java and choose R un .

    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...]



 



Copyright © 1997, 2009, Oracle. All rights reserved.

false ,,,,,,,,,,,,,,,