Part 4: Testing the Facade Inside and Outside the Java EE Container
In this section you add a new method to the entity bean using the EJB 3.0 annotation technique. You then test the new method by creating a facade client and running it.

To test your EJBs you need to run a client program that can create or find EJB instances and call their remote interface methods. JDeveloper provides a sample client utility that will help you create clients quickly. You can run and test EJBs using either the integrated server or a remote server;

Step 1: Add a New Method to the Entity and Expose it
  1. Click the Red button and select the IntegratedWebLogicServer to stop WebLogicServer.

    Stopping WebLogicServer
  2. The server is stopped when you see the following message in the log window.

    Stopping WebLogicServer
  3. Collapse the ViewController project and expand the Model project.

    Application Navigator
  4. Double-click Employees.java to open the source code for the class and expand the @Entity node to display the hidden code.

    The Entity bean java code
  5. Add a comma at the end of the last @NamedQuery statement, then add a query to the class that retrieves employees by name. Add the following statement:


    So that the code looks like the following:

    @Entity
    @NamedQueries( {
    @NamedQuery(name = "Employees.findAll", query = "select o from Employees o"),
    @NamedQuery(name = "Employees.findBySal", query = "select o from Employees o where o.salary > :p_sal"),
    @NamedQuery(name = "Employees.findByName", query = "select o from Employees o where o.firstName like :p_name")
    })

    The Entity bean java code

    What makes these objects different from other Java files are the annotations that identify them as EJB entities.Show more or lessRead more...
    A key feature of EJB 3.0 and JPA is the ability to create entities that contain object-relational mappings by using metadata annotations rather than deployment descriptors as in earlier versions.

  6. Click the Make icon to compile the Employees.java class.

    The Make icon

    Make sure that the Message - Log window does not report any errors.

    Log window messages

  7. Add the new method to the session bean as follows:

    Right-click the HRFacadeBean node in the Applications window and select Edit Session Facade from the context menu.

    Context menu

  8. Expand the Employees node of the dialog. Notice that the new named query getEmployeesFindByName appears as an exposable method. Select getEmployeesFindByName, deselect getEmployeesFindBySal and click OK.

    Specify Session Facade Options dialog
  9. JDeveloper provides a way to test the EJB by creating a sample client. Normally, you would right click HRFacadeBean and select New Sample Java Client from the context menu. HOWEVER, due to a bug in the current version of JDeveloper, the option to generate a sample client from the context menu will not function properly. The following steps help you fix this problem. For more details on this issue, refer to the release notes..

  10. In the Applications window, right-click the HRFacadeBean.java and select New Sample Java Client...

  11. In the Create Sample Java Client pane, leave the values at their default and click OK.

  12. Two things need to be fixed.

    First since the method uses a parameter, we need to add a value for the parameter.
    In the code, scroll to the "/* FIXME: Pass parameters here */". In the getEmployeesFindByName method, add "p%" as the parameter. When you're finished, your code should look like the code below.

    (List<Employees>) hRFacade.getEmployeesFindByName("p%") /* FIXME: Pass parameters here */)


    HRFacadeClient Java code

  13. Second, copy the following code and paste it at the end of the class.
    Some import statements will also be included into your code


      private static Context getInitialContext() throws NamingException {
        Hashtable env = new Hashtable();
        // WebLogic Server 10.x connection details
        env.put( Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory" );
        env.put(Context.PROVIDER_URL, "t3://127.0.0.1:7101");
        return new InitialContext( env );
      }

  14. Click the Save All Save All button icon to save your work.

  15. Right click the HRFacadeBean in the Applications window and select Run from the context menu to launch the facade bean in WebLogicServer.

    Context menu

    Wait until the WebLogicServer is started.

    Running Log window

    The integrated Oracle WebLogic Server runs within JDeveloper. You can run and test EJBs quickly and easily using this server, and then deploy your EJBs with no changes to them.
  16. Then, right click HRFacadeClient and select Run from context.

    Context menu

  17. The Log window returns the database data based on the three methods the client contains (getDepartmentsFindAll(), getEmployeesFindAll(), and getEmployeesFindByName().)

    Returned info in the Running log window

  18. To better display the results of the findByName() method, in the HRFacadeClient.java class, comment out the for loop corresponding to the getEmployeesFindAll() method, and comment out the for loop corresponding to the getDepartmentsFindAll() method. Your code should look something like this:

    HRFacadeClient Java code

  19. Click the Make button Make button to recompile the class, and ensure that no errors are returned.

  20. Right click the HRFacadeClient class and select Run from context.

    Context menu

  21. The Log window should now display the returned rows retrieved by your ' P%' clause.

    Returned info in the Running Log window

Step 2: Run the Java Service outside the Java EE container
A persistence unit can be configured to run inside or outside the container. In EJB 3.0, you can run entities in a pure Java SE environment, without using an application server. One reason you might do this is to create a simple Java SE testbed (using JUnit, perhaps) to test your entity behavior without the overhead of deploying/executing in an application server. Another reason is you may want to run a Swing application locally.
In this section, you create a session bean that implements a method to find employee and department records.
  1. Create a new persistence unit to run the java service outside the Java EE container.

    Right-click the META-INF > persistence.xml and select New Java Service Facade from the context menu.

    Context menu

  2. In the Java Service Class panel, you can choose to create a new persistence unit (in the next panel) or use an existing unit. Select Choose a Persistence Unit or Create one in the next Panel, check the Generate a Sample Java Client checkbox and click Next.

    Step 1 of the Java Service Facade Methods

  3. Name the the Persistence Unit outside. Choose JDBC Connection and make sure the JDBC connection is set to HR. Click Next.

    Step 2 of the Java Service Facade Methods

  4. All methods should be selected by default. Select only the following methods and click OK.

  5. Step 3 of the Java Service Facade Methods

    Click Next then Finish.

  6. In the source editor window, for the JavaServiceFacadeClient class, add "P%" as a parameter to the getEmployeesFindByName method so that the statement is:

    (List<Employees>)javaServiceFacade.getEmployeesFindByName("P%")

    JavaServiceFacadeClient class
  7. Click the Make Make icon button to compile the class and save your work.

  8. Right-click the JavaServiceFacadeClient node in the Applications window and select Run from context.

    Context menu

  9. The Log window displays the result of the execution of the class running outside Java EE container, returning the lastName of the first of the retrieved records (Pat Fay).

    The Running Log window

  10. Double-click the META-INF > persistence.xml node to display the contents of the file.

    Application Navigator

  11. The persistence editor opens showing the Model - Persistence Unit by default. Click the bread crumb navigation button to show both persistence units.

    Persistence Units

    Persistence Units
  12. Click the Source tab to show the code for both persistence units that have been created. The Model one and the outside one.

    The source code for persistences
Summary
In this tutorial you discovered how to use EJB/JPA in a Web Application. You learned how to: To learn more about developing Java EE applications, refer to:

Bookmark Print Expand all | Hide all
Back to top

Did you find this page helpful?



Copyright © 2013, Oracle and/or its affiliates. All rights reserved.