tuto-oracle.pdf

12
Building a Web Application Using EJB, JPA, and JavaServer Faces In this tutorial, you will use the rich functionalities provided within JDeveloper 11.1.2 to create a web application based on EJB, JPA and JavaServer Faces. Purpose Duration Application In this tutorial, you use Oracle JDeveloper 11g Version 11.1.2.0.0 to build a web application. To build the data model, you use the EJB diagrammer, EJB 3.0 and Java Persistence API (JPA). For the web client side, JavaServer Faces (JSF) is used. A main master-detail page is created with query and edit functionalities in the user interface. A task flow, with a search functionality, is also added as a region to the page. To see the complete application you will create, click the Download button to download a zip of the final application, and then unzip it in your JDeveloper mywork folder. 120 minutes Part 1: Building the Data Model with EJB 3.0 You first build the data model portion of the application. The Java Persistence API (JPA) provides a POJO persistence model for object- relational mapping. Step 1: Create a New Application and Projects Before you create any components, you must first create an application and projects. Start JDeveloper by selecting Start > All Programs > Oracle Fusion Middleware 11.1.2.0.0 > JDeveloper Studio 11.1.2.0.0 1. If prompted for a Role, choose Studio Developer (All Features) and click OK. 2. Close the Tip of the Day window. 3. Click the Application Navigator tab to go back to the Application Navigator. Click the New Application icon. 4. In the New Gallery, select the Fusion Web Application (ADF) option. 5. You build the model using EJB/JPA components. Once loaded, the JDeveloper IDE appears. Page 1 of 12 Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, JPA... 08/03/2012 http://docs.oracle.com/cd/E18941_01/tutorials/jdtut_11r2_51/jdtut_11r2_51_1.html

Transcript of tuto-oracle.pdf

Building a Web Application Using EJB, JPA, and JavaServer Faces

In this tutorial, you will use the rich functionalities provided within JDeveloper 11.1.2 to create a web application based on EJB, JPA and JavaServer Faces.

Purpose Duration Application

In this tutorial, you use Oracle JDeveloper 11g Version 11.1.2.0.0 to build a web application. To build the data model, you use the EJB diagrammer, EJB 3.0 and Java Persistence API (JPA). For the web client side, JavaServer Faces (JSF) is used. A main master-detail page is created with query and edit functionalities in the user interface. A task flow, with a search functionality, is also added as a region to the page. To see the complete application you will create, click the Download button to download a zip of the final application, and then unzip it in your JDeveloper mywork folder.

120 minutes

Part 1: Building the Data Model with EJB 3.0

You first build the data model portion of the application. The Java Persistence API (JPA) provides a POJO persistence model for object-relational mapping.

Step 1: Create a New Application and Projects

Before you create any components, you must first create an application and projects.

Start JDeveloper by selecting Start > All Programs > Oracle Fusion Middleware 11.1.2.0.0 > JDeveloper Studio 11.1.2.0.01.

If prompted for a Role, choose Studio Developer (All Features) and click OK.2.

Close the Tip of the Day window. 3.

Click the Application Navigator tab to go back to the Application Navigator. Click the New Application icon. 4.

In the New Gallery, select the Fusion Web Application (ADF) option. 5.

You build the model using EJB/JPA components.

Once loaded, the JDeveloper IDE appears.

Page 1 of 12Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, JPA...

08/03/2012http://docs.oracle.com/cd/E18941_01/tutorials/jdtut_11r2_51/jdtut_11r2_51_1.html

Click OK.

In the Create Application dialog box, enter the Application Name HR_EJB_JPA. Notice that as you enter the application name, the directory name changes automatically. Enter oracle as the Application Package Prefix.

6.

Click Finish. Wait for JDeveloper to create your application environment.

The Application Navigator displays the two projects created. On the right pane, notice the application overview allowing you to keep track of the steps as you complete them.

7.

Step 2: Create the Persistence Model

In this section of the tutorial, you create the persistence model for Departments and Employees using EJB 3.0 entity beans. To create EJB 3.0 entity beans, perform the following steps:

In the Application Navigator, right-click the Model node and select New from the context menu.

1.

In the New Gallery select the All Features tab, then select Business Tier > EJB as the category and double-click the Entities from Tables item.

2.

Choosing the Fusion Web Application option creates 2 projects.

There are now 2 projects in your application.

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

08/03/2012http://docs.oracle.com/cd/E18941_01/tutorials/jdtut_11r2_51/jdtut_11r2_51_1.html

In Select EJB Version, select EJB 3.0 -- JPA Entities as the EJB version if not already selected, then Next. 3.

Click Next to skip the persistence unit definition.

In the Type of Connection page choose the Online Database Connection option. Then click Next. 4.

In the Database Connection Details page, click the Create button to create a new connection. 5.

EJB 3.0 greatly simplifies entity beans and standardizes the POJO persistence model.

Page 3 of 12Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, JPA...

08/03/2012http://docs.oracle.com/cd/E18941_01/tutorials/jdtut_11r2_51/jdtut_11r2_51_1.html

Provide the following values to create the database connection to the hr schema. Note that the values provided here as examples may need to be modified to work with your environment. Leave the rest of the properies at their default.

6.

Option Value

Connection Name HR

Username/Password hr / <your hr password>

Save Password Checkbox checked

Driver Thin

Host Name The machine where the schema is installed (e.g. localhost)

SIDThe database where the HR schema is installed (XE, orcl...)

Click Test Connection to confirm that you can connect. 7.

Back in the Database Connection Details page, click Next. 8.

In the Select Tables page, click Query to retrieve the available objects for the HR schema. Then select DEPARTMENTS and

EMPLOYEES and shuttle the selection into the Selected pane using the right arrow button .

9.

Click OK if the connection was successful.

Page 4 of 12Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, JPA...

08/03/2012http://docs.oracle.com/cd/E18941_01/tutorials/jdtut_11r2_51/jdtut_11r2_51_1.html

Click Next.

In this step, make sure the package name is oracle.model. 10.

Click Next, then Finish.

In the Application Navigator one java class file is created for Departments and one for Employees.11.

Click the Save All icon to save your work.12.

Step 3: Create an EJB Diagram

An EJB diagram is a way to create and visualize entities, java beans and relationships between these components. To create an EJB diagram perform the following steps:

Right click the Model node in the Application Navigator and select New. 1.

In the New Gallery select Business Tier > EJB as the category and double click EJB Diagram (JPA/EJB 3.0). 2.

Page 5 of 12Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, JPA...

08/03/2012http://docs.oracle.com/cd/E18941_01/tutorials/jdtut_11r2_51/jdtut_11r2_51_1.html

In the Create EJB Diagram dialog, change the default name for the diagram (EJB Diagram1) to HR EJB Diagram.The default Package name should be oracle.model.

3.

Click OK.

On the Associate Diagram With Persistence Unit dialog, click OK to accept the proposed Persistence Unit (Model.jpr).4.

A new empty diagram opens in the diagram editor.5.

Page 6 of 12Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, JPA...

08/03/2012http://docs.oracle.com/cd/E18941_01/tutorials/jdtut_11r2_51/jdtut_11r2_51_1.html

b

Select the Departments and Employees entities from the Application Navigator and then drag and drop them onto the diagram.6.

The diagram displays the EJB components that you just created from the Departments and Employees tables. Zoom in if necessary and reorganize the layout of the diagram to have both entities horizontally aligned. You can tidy the diagram by selecting relationships and using the Straighten lines option from the context menu.

7.

Click the Save All icon to save your work.8.

Step 4: Create a Session Bean to Expose for the Client Application

In this step, you create a session bean that implements a method to find employees and departments records.

In the Component Palette, select the EJB Components library and open the EJB Nodes. 1.

Modeling EJB/JPA components on a diagram can be useful for the developer.

A session facade presents client objects with a unified interface to the underlying EJBs (Enterprise JavaBeans). The client interacts only with the facade, which resides on the server and invokes the appropriate EJB methods.

Page 7 of 12Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, JPA...

08/03/2012http://docs.oracle.com/cd/E18941_01/tutorials/jdtut_11r2_51/jdtut_11r2_51_1.html

Select the Session Bean component and then drag and drop it onto the diagram. 2.

The Create Session Bean Wizard opens. (If necessary, click Next on the Welcome step.)

In the EJB Name and Options step, set the EJB Name to HRFacade and make sure that the following values are properly set: 3.

Property Value

EJB Name HRFacade

Session Type Stateless

Transaction Type Container

Generate Session Facade Method

(Checked)

Entity Implementation JPA Entities

Persistence Unit Model

Click Next.

In the Session Facade step, any entities in this project appear as a node in the tree control. You can select the checkbox to include all entity methods this entity exposes, or expand the nodes and select a subset of methods. Expand the Employees and Departments nodes and select all methods for each entity.

4.

There are two types of session bean, Stateful and Stateless.

Page 8 of 12Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, JPA...

08/03/2012http://docs.oracle.com/cd/E18941_01/tutorials/jdtut_11r2_51/jdtut_11r2_51_1.html

Click Next.

In the Class Definition step, make sure that the full name for Bean Class is oracle.model.HRFacadeBean, and then click Next. 5.

In the following step, ensure that both Remote and Local interface implementations are selected. The remote interface is used for client applications that run in a separate virtual machine, such as Java clients, whereas a local interface is used for client applications that run in the same virtual machine, such as Web clients.

6.

Click Next to review the summary of the created classes and then Finish.

The diagram should now look like the following:7.

The Application Navigator should look like this:8.

Page 9 of 12Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, JPA...

08/03/2012http://docs.oracle.com/cd/E18941_01/tutorials/jdtut_11r2_51/jdtut_11r2_51_1.html

The session bean is made up of three files: HRFacadeBean - contains the session bean code. HRFacade - describes the capabilities of the bean for remote clients and HRFacadeLocal describes the capabilities for the local client.

Click the Save All icon to save your work.9.

Step 5: Create ADF Data Controls from EJB Session Beans

You now expose the EJB as a data control for the Oracle ADF framework. This simplifies the way that you bind user interfaces to the EJB. To learn more about the ADF Framework visit: http://oracle.com/technology/products/adf To create ADF data controls from EJB Session Beans, perform the following steps:

Right-click the HRFacadeBean node in the Application Navigator and select Create Data Control from the context menu. 1.

In the Choose EJB Interface dialog, select Local, and click OK. 2.

Click the Save All icon to save your work.

The Application Navigator should now look like this:3.

Page 10 of 12Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, J...

08/03/2012http://docs.oracle.com/cd/E18941_01/tutorials/jdtut_11r2_51/jdtut_11r2_51_1.html

Expand the Data Controls accordion to review the components that have been created and are now available for the UI. 4.

Notice the Data Control Registry displaying the available data controls. 5.

Expand the HRFacadeLocal node. Selecting the departmentsFindAll node provides details in the right hand side about the data control.

6.

You can collapse the Model project node and right-click any of the tabs to close all open tabs. 7.

A number of things happen in your project when you create a data control.

The overview editor for the DataControls.dcx file provides a view of the master-detail hierarchies of your data model as well as methods from the session facade.

Page 11 of 12Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, J...

08/03/2012http://docs.oracle.com/cd/E18941_01/tutorials/jdtut_11r2_51/jdtut_11r2_51_1.html

Previous 1 2 3 4 Next

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

Page 12 of 12Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, J...

08/03/2012http://docs.oracle.com/cd/E18941_01/tutorials/jdtut_11r2_51/jdtut_11r2_51_1.html