Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, JPA, And...

download Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, JPA, And JavaServer Faces Page 4

of 12

Transcript of Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, JPA, And...

  • 7/30/2019 Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, JPA, And JavaServer Faces Page 4

    1/12

    5/17/13 Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, JPA, and JavaServer Faces

    docs.oracle.com/cd/E18941_01/tutorials/jdtut_11r2_51/jdtut_11r2_51_4.html

    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 run it inside or outside the Java EE container.

    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 square Terminate button and select the HR_EJB_JPA to stop the running application.

    2. Click the Red button and select the IntegratedWebLogicServerto stop WebLogicServer.

    3. Collapse the ViewControllernode and expand the Model node.

    4. Double click the Employees entity bean on the diagram to open the source code for the class and expand the @Entity node to display the

    hidden 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

  • 7/30/2019 Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, JPA, And JavaServer Faces Page 4

    2/12

    5/17/13 Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, JPA, and JavaServer Faces

    docs.oracle.com/cd/E18941_01/tutorials/jdtut_11r2_51/jdtut_11r2_51_4.html 2

    following statement:

    So that the code looks l ike 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")

    })

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

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

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

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

    @NamedQuery(name = "Employees.findByName", query = "select o from Employees o where o.firstName like

    :p_name")

    What makes these objects different from other Java files are the annotations that identify them as EJB entities.

    A key feature of EJB 3.0 and JPA is the ability to create entities that contain object-relational mappings by using metadata annotationsrather than deployment descriptors as in earlier versions.

  • 7/30/2019 Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, JPA, And JavaServer Faces Page 4

    3/12

    5/17/13 Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, JPA, and JavaServer Faces

    docs.oracle.com/cd/E18941_01/tutorials/jdtut_11r2_51/jdtut_11r2_51_4.html 3

    8. Expand the Employees node of the dialog. Notice that the new named query getEmployeesFindByName appears as an exposable method.

    Select it and unselect the getEmployeesFindBySal one.

    Click OK.

    Save all your work.

    9. JDeveloper provides a way to test the EJB by creating a sample client. To do so, right click HRFacadeBean and select New Sample Java

    Client from the context menu.

  • 7/30/2019 Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, JPA, And JavaServer Faces Page 4

    4/12

    5/17/13 Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, JPA, and JavaServer Faces

    docs.oracle.com/cd/E18941_01/tutorials/jdtut_11r2_51/jdtut_11r2_51_4.html 4

    10. Select IntegratedWebLogicServeras the Application Server Connection.

    Then click OK.

    11. Review the code of the HRFacadeClient class and correct the reported error for the getEmployeesFindByName() method by adding a value

    parameter"P%" so that it looks like the following:

    Also, remove the /*FIX ME: Pass parameters here */ comment.

    Click the Save All icon to save your work.

    12. Right click the HRFacadeBean in the Application Navigator and select Run from the context menu to launch the facade bean in

    WebLogicServer.

    The client is created and opens in the code editor.

    If your session bean serves as a facade over JPA entities, code is generated to instantiate the query methods. If you exposed methods

    on your bean, the generated client contains methods that can be uncommented to call them.

  • 7/30/2019 Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, JPA, And JavaServer Faces Page 4

    5/12

    5/17/13 Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, JPA, and JavaServer Faces

    docs.oracle.com/cd/E18941_01/tutorials/jdtut_11r2_51/jdtut_11r2_51_4.html 5

    Wait until the WebLogicServer is started.

    13. Right click HRFacadeClient and select Run from context.

    14. The Log window returns the database data based on the three methods the client contains.

    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.

  • 7/30/2019 Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, JPA, And JavaServer Faces Page 4

    6/12

    5/17/13 Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, JPA, and JavaServer Faces

    docs.oracle.com/cd/E18941_01/tutorials/jdtut_11r2_51/jdtut_11r2_51_4.html 6

    15. To better display the results of the findByName() method, in the HRFacadeClient.java class, comment out the forloop corresponding to

    the getEmployeesFindAll() method, and comment out the forloop corresponding to the getDepartmentsFindAll() method. Your code

    should look something like this:

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

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

  • 7/30/2019 Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, JPA, And JavaServer Faces Page 4

    7/12

    5/17/13 Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, JPA, and JavaServer Faces

    docs.oracle.com/cd/E18941_01/tutorials/jdtut_11r2_51/jdtut_11r2_51_4.html 7

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

    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.

  • 7/30/2019 Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, JPA, And JavaServer Faces Page 4

    8/12

    5/17/13 Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, JPA, and JavaServer Faces

    docs.oracle.com/cd/E18941_01/tutorials/jdtut_11r2_51/jdtut_11r2_51_4.html 8

    2. In the Java Service Class panel, you can select 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, and check the Generate a Sample Java Client checkbox.

    Click Next.

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

    Click Next.

  • 7/30/2019 Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, JPA, And JavaServer Faces Page 4

    9/12

    5/17/13 Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, JPA, and JavaServer Faces

    docs.oracle.com/cd/E18941_01/tutorials/jdtut_11r2_51/jdtut_11r2_51_4.html 9

    4. All methods should be selected by default. Deselect some of them so that your selection looks like the following image.

    Click Next then Finish.

    5. In the source editor window, for the JavaServiceFacadeClientclass, add "P%" as a parameter to the getEmployeesFindByName method

    so that the statement is:

    (List)javaServiceFacade.getEmployeesFindByName("P%")

    6. Click the Make button to compile the class and save your work.

    7. Right-click the JavaServiceFacadeClient node in the Application Navigator and select Run from context.

  • 7/30/2019 Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, JPA, And JavaServer Faces Page 4

    10/12

    5/17/13 Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, JPA, and JavaServer Faces

    docs.oracle.com/cd/E18941_01/tutorials/jdtut_11r2_51/jdtut_11r2_51_4.html 10

    8. 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).

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

  • 7/30/2019 Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, JPA, And JavaServer Faces Page 4

    11/12

    5/17/13 Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, JPA, and JavaServer Faces

    docs.oracle.com/cd/E18941_01/tutorials/jdtut_11r2_51/jdtut_11r2_51_4.html 1

    10. Both persistence units are described. The default inside one and the newly-created for outside Java EE run. Click the Source tab to review

    details.

    11. The source code shows both persistence units that have been created. The Model one and the outside one.

    Summary

    In this tutorial you discovered how to use EJB/JPA in a Web Application. You learned how to:

    Build the data model with EJB 3.0 Using the EJB diagrammer

  • 7/30/2019 Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, JPA, And JavaServer Faces Page 4

    12/12

    5/17/13 Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, JPA, and JavaServer Faces

    Build the view project

    Add and expose a new method to the UI

    Test the facade bean inside and outside the Java EE container

    To learn more about developing Java EE applications, refer to:

    "Developing with EJB and JPA Components" in User's Guide for Oracle JDeveloper

    Java EE Developer's Guide for Oracle ADF

    Programming Enterprise JavaBeans for Oracle WebLogic Server 11g

    Previous 1 2 3 4 Next

    Copyright 2011, Oracle and/or its aff iliates. All rights reserved.

    http://docs.oracle.com/cd/E18941_01/tutorials/jdtut_11r2_51/jdtut_11r2_51_3.htmlhttp://docs.oracle.com/cd/E18941_01/tutorials/jdtut_11r2_51/jdtut_11r2_51_2.htmlhttp://docs.oracle.com/cd/E18941_01/tutorials/jdtut_11r2_51/jdtut_11r2_51_1.htmlhttp://docs.oracle.com/cd/E18941_01/tutorials/jdtut_11r2_51/jdtut_11r2_51_3.htmlhttp://download.oracle.com/docs/cd/E17904_01/web.1111/e13719/toc.htmhttp://www.oracle.com/pls/as111200/lookup?id=ADFJEhttp://www.oracle.com/pls/as111200/lookup?id=OJDUG954