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

The application to build reflects the Model-View-Controller architecture. The model is provided by EJB Components, while the view and controller are provided by JavaServer Faces.You use the ADF Faces set of JSF-compatible components to build a richer web interface.

This tutorial requires to have JDeveloper 12.1.3. installed and to have access to an Oracle database 10g and onward with access to the HR schema.

Purpose Duration Application

In this tutorial, you use Oracle JDeveloper 12c Version 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.

90 minutes Download application
Part 1: Building the Data Model with EJB 3.0
In this first section of the tutorial, you build the data model portion of the application. The Java Persistence API (JPA) provides a POJO persistence model for object-relational mapping. You build the model using EJB/JPA components. JDeveloper includes step-by-step wizards for creating EJB projects, entities, persistence units, session beans, and message-driven beans. You can build entities from online or offline database definitions and from application server data source connections. There is also seamless integration with JPA and TopLink technology to provide a complete persistence package.
Step 1: Create a New Application and Projects
Before you create any components, you must first create an application and projects.
  1. Start JDeveloper by selecting Start > Programs > <JDEVELOPER_HOME> > OracleHome > Oracle JDeveloper Studio > Oracle JDeveloper Studio

    If a dialog box opens asking if you would like to import preferences from a previous JDeveloper installation, click NO.

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

    Select Role dialog at startup
  3. Close the Tip of the Day window.

    Once loaded, the JDeveloper IDE appears. The very first time you open JDeveloper, the Start Page displays. You can re-invoke the Start Page later by choosing Help > Start Page.

    JDeveloper Start Page

    Notice the various options available to help you learn about JDeveloper. After exploring these options, click the X on the Start Page tab to close it. (the X appears when you mouse over the tab).

  4. Click the Application tab to go back to the Applications window. Click the New Application icon.

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

    The New Gallery dialog

    Click OK.

  6. In the Name your 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.

    Step 1 of the  Create Fusion Web Application
    Selecting the Fusion Web Application option creates 2 projects.
    Clicking Next on each page of the wizard and leaving the default values shows you the project names and package names that are going to be used. It also allows you to select specific features for the Model and ViewController projects.
    The default name for the first project is Model as the project name and oracle.model as the package name.
    The default name for the second project is ViewController as the project name and oracle.view as the package name..
  7. Click Finish. Wait for JDeveloper to create your application environment.

  8. The Applications window 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.

    Default application Navigator created for a Fusion Web Application

    There are now 2 projects in your application. The Model project is empty and will be populated with EJB JPA components after the creation of a database connection.
    The ViewController project contains the basic components for building the UI.

    The Overview pane guides you through your application development. You can use it and follow the step-by-step instructions to build your applications.

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:
  1. In the Applications window, right-click the Model node and select New > From Gallery from the context menu.

    New option from context

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

    This is the New Gallery dialog.

    Click OK.

  3. In the Create Entities from Tables wizard, select JPA 2.0 Entities (Java EE6), then click Next.

    Step 1 of the Create Entities from Tables
  4. Click Next to skip the persistence unit definition.

  5. In the Type of Connection page select the Online Database Connection option. Then click Next.

    Step 3 of the Create Entities from Tables
  6. In the Database Connection Details page, click the Create button Add Button to create a new connection.

    Step 4 of the Create Entities from Tables to define a database connection
  7. 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.

    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)

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

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

    Create Database Connection dialog

    Click OK if the connection was successful.

    If the connection fails, check that the parameters entered are correct and that the Oracle database and listener services are running.
    The connection should also appear in the Initialize Business Components Project page.
  9. Back in the Database Connection Details page, click Next.

    Step 4 of the Create Entities from Tables
  10. 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 Right Arrow button.

    Step 5  of the Create Entities from Tables

    Click Next.

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

    Step 6 of the Create Entities from Tables
  12. Click Next, then Finish.

  13. In the Applications window one java class file is created for Departments and one for Employees.

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

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:

  1. Right click the Model node in the Applications window and select New > From Gallery.

    The New option from context
  2. In the New Gallery select Business Tier > EJB as the category and double click EJB Diagram (JPA/EPA 3.x).

    The New Gallery
  3. Click OK.

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

     The Create EJB Diagram dialog
  5. Click OK.

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

    Associate Diagram With Persistence Unit dialog
  7. A new empty diagram opens in the diagram editor.

    New empty diagram b
    Modeling EJB/JPA components on a diagram can be useful for the developer. Show more or lessRead more... Enterprise JavaBeans (EJBs) modeling helps you visualize your EJB entity relationships and architecture, and to quickly create a set of beans to populate with properties and methods, and to create a graphical representation of those beans and the relationships and references between them. Whenever a bean is modeled, the underlying implementation files are also created.
  8. Select the Departments and Employees entities from the Applications window and then drag them onto the diagram.

    Drag and Drop EJBs onto the diagram surface
  9. 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.

    EJB diagram  with entities
  10. Click the Save All Save All button icon to save your work.

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

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. As a result, dependencies and communication between clients and EJBs are reduced. If you are performing remote access without a session facade, numerous remote calls are needed for the clients to access EJB 3.x entities directly over the network. This results in a large amount of network traffic that negatively affects performance. In addition, without a facade the client depends directly on the implementation of the business objects, so that if the interface of an EJB changes, client objects have to be changed as well.

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

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

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

    Drag and drop Session Bean onto the EJB diagram

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

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

    Property Value
    EJB Name HRFacade
    Session Type Stateless
    Transaction Type Container
    Generate Session Facade Method (Checked)
    Entity Implementation JPA Entities
    Persistence Unit Model

    This is Step 2 of the Create Session Bean wizard.
  4. Click Next.

    There are two types of session bean, Stateful and Stateless. Show more or less Read more...

    A stateful session bean maintains conversational state on behalf of the client. A conversational state is defined as the session bean field values plus all objects reachable from the session bean fields. Stateful session beans do not directly represent data in a persistent data store, but they access and update data on behalf of the client. The lifetime of a stateful session bean is typically that of its client.

    Stateless session beans are designed strictly to provide server-side behavior. They are anonymous because they contain no user-specific data. The EJB architecture provides ways for a single stateless session bean to serve the needs of many clients. All stateless session bean instances are equivalent when they are not involved in serving a client-invoked method. The term stateless means that it does not have any state information for a specific client. However, stateless session beans can have non-client specific state, for example, an open database connection.

    Use Container for a transaction being handled by the session facade and bean for managing user transaction explicitly through a programmatic process.
  5. In the Session Facade - Select JPA Entity Method 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.

    This is Step 3 of the Create Session Bean wizard.
  6. Click Next.

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

    This is Step 4 of the Create Session Bean wizard.
  8. 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.

    This is Step 5 of the Create Session Bean wizard.

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

  9. The diagram should now look like the following:

    The EJB diagram
  10. The Applications window should look like this:

    The Application Navigator

    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.

  11. Click the Save All The Save All icon icon to save your work.

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:
To create ADF data controls from EJB Session Beans, perform the following steps:

  1. Right-click the node in the Applications window and select Create Data Control from the context menu.

    The context menu
  2. In the first step of the wizard, click Next to accept the default name (HRFacadeBean)

  3. In the Session EJB Business Interface page, select Local, and click Finish..

    Choose EJB Interface dialog.

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

  4. The Applications window should now look like this, with a DataControl.dcx file part of the Model project:

    The Application Navigator

    A number of things happen in your project when you create a data control. When you create a data control based on an EJB session bean, JDeveloper creates the data control definition file (DataControls.dcx), opens the file in the overview editor, and displays the file's hierarchy in the Data Controls panel. This file enables the data control to work directly with the service and the bindings.

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

    The Data Controls pane
  6. Notice the Data Control Registry displaying the available data controls.

    The Data Control Registry
  7. Expand the HRFacadeLocal node. Selecting the departmentsFindAll node provides details in the right hand side about the data control.

    The Data Control Registry

    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.
    When you select a node, you can view the bean properties for the corresponding entity class in the Attributes tab. In the Accessors tab, you can view all bean properties for the corresponding entity class that return a collection. In the Operations tab, you can view other methods from the session facade that operate on the entities.

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

    Context menu
Step 6: Add UI Hints and Validation Rules

The ADF Model provides a declarative way of adding validation and consistent UI control to your application. Once you create a data control, you have access to a fine level of control over display and validation characteristics of that data control.

In the next few steps, you will set the label of several attributes and and a validation rule to the Employees data control.

  1. In the Data Controls panel, right-click departmentsFindAll and select edit definition.

    The context menu
  2. Select the Attributes node.

    The context menu
  3. Select departmentId in the attributes list and click the UI Hints tab.

    Choose EJB Interface dialog.
  4. Enter Department Id in the Label property for the departmentId attribute.

    The Application Navigator
  5. Repeat the previous step for departmentName and locationId. Set the departmentName Label to Department Name and the locationId Label to Location ID.

  6. Next, we'll add some validation to the Employees data control.

  7. In the Data Controls panel, right-click employeesFindAll and select Edit Definition.

    The Data Control Registry
  8. Select salary in the attributes list and click the Validation Rules tab.

    The Data Control Registry
  9. Click the green plus sign Add Button on the right side of the Validation Rules pane.

    The Data Control Registry
  10. In the Add Validation Rule for: salary dialog, enter the following values:

    Option Value
    Description Salary between 100 and 15000
    Type Range
    Operator Between
    Minimum Value 100
    Maximum Value 15000

    The Data Control Registry
  11. Click the Failure Handling tab and enter "Salary amount must be between 100 and 15000." as the Message Text and click OK to create the validation rule.

    The Data Control Registry
  12. The Employees data control definition should now look like the following:

    The Data Control Registry

  13. Save your work.

  14. You have now completed the model including added UI Hints and validation rules. The UI Hints will be used on any new page uses the departments data control The salary validation rule will be enforced on any page that uses salary.

Bookmark Print Expand all | Hide all
Back to top
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.