Part 2: Building the Data Model
In this section of the tutorial, you create the application and projects that contain the data model and client interface portions of your application.
show more or lessRead more...
Step 1: Create an Application and Projects

    An enterprise application project ties together one or more Java EE modules, including application client modules, JPA modules, Connector modules, or Web modules. To create a new Oracle ADF application, perform the following steps:

  1. Right-click within the Project Explorer pane and select New > ADF Application. (Or you can select File > New > ADF Application.)

  2. alt text
  3. In the New Oracle ADF Application Wizard, enter HRApp as the application name. Notice that the default name for the dynamic web project is HRAppWeb. Next to the JPA Project field, click  New JPA Project.

  4. alt text
  5. In the New JPA Project Wizard:

    Note that the default Project name is HRAppModel.

    Select Oracle WebLogic Server 11gR1 (10.3.6) as the Target runtime if it is not already the default value.

    Make sure the JPA version is 2.0.

    Under Configuration, select Oracle EJB Model Project.

    Click Next.

  6. alt text
  7. Click Next to accept the defaults on the Java page.

  8. alt text
  9. In the JPA Facet page:

    Select EclipseLink 2.1.x as the platform.

    Select WebLogic System Library (Oracle Toplink 11gR1) as the type.

    Select HR as the connection.

    Click Finish.

  10. alt text
  11. Eclipse prompts you to open the JPA perspective for this new project. Click No to continue using the Java EE perspective that you selected earlier.

  12. alt text
  13. Click Finish to create the application and projects.

  14. alt text
  15. Now that you have created the application and projects, the Project Explorer should look like the following:

  16. alt text

    In the next few steps, you explore the application you just created, including a few of the generated artifacts.

  17. Expand the HRApp node to open the Enterprise Application Project (called an EAR project in Eclipse). Expand EarContent > adf > META-INF.

  18. alt text
  19. Double-click adf-config.xml to open the file, and then click the Source tab to see the source view.

  20. alt text
  21. In the Project Explorer, expand all the nodes under EarContent.

  22. alt text
  23. Notice that the ADF library, adf-loc.jar, has been added to your project. Open and examine the application.xml configuration file. Notice that it includes a reference to the HRAppWeb.war file and the ejb model project HRAppModel.jar.

  24. alt text
  25. Open the weblogic-application.xml file. In the Design tab, expand Shared Libraries and select adf.oracle.domain.

  26. alt text
  27. Next to the Implementation version field, click the Browse icon and select 11.1.1.2.0. Click OK.

  28. alt text
  29. Click the Save All icon to save the changes.

  30. Click the Source tab, and notice the declared components, such as listeners and libraries.

  31. alt text
  32. Back in the Project Explorer, expand the HRAppModel node, and review the basic structure that was created for the model project.

  33. alt text
  34. Expand the HRAppWeb node, and review the basic structure that was created for the user interface project.

  35. alt text
  36. When you are done exploring the project structure and artifacts, right-click any tab and select Close All to close all the open tabs in the editor pane.

  37. alt text
Step 2: Create the Persistence Model

The Java Persistence API, sometimes referred to as JPA, is a Java programming language framework that manages relational data in Java applications. In this section, you use OEPE wizards to create Java classes, known as entities, which map database table definitions to entity objects. The entities use JPA to manage data persistence.

Before you can select tables on which to base your JPA entities, the connection that you defined earlier to the HR database must be active.

  1. In the bottom pane, select the Data Source Explorer tab.

  2. alt text
  3. Under Database Connections, right-click HR, and select Connect.

  4. alt text
  5. When the connection is successful, the Data Explorer shows the database SID and allows you to expand and explore the contents of the database schema.

  6. alt text
  7. In the Project Explorer, collapse all the nodes except for HRAppModel. Right-click the HRAppModel node and select JPA Tools > Generate Entities from Tables.

  8. alt text
  9. In the Generate Custom Entities Wizard, select HR as the connection and HR as the schema. Notice that selecting these options populates the Tables list. In the Tables list, select the following three tables: Employees, Departments, and Locations. The data model for the application is based on these tables from the HR schema. Click Next.

  10. alt text
  11. Notice the table associations that are derived from the database schema definitions. You can select any of the relationships to examine the association details and properties. By default, the wizard creates the associations. Click Next to accept the default table associations and continue.

  12. alt text
  13. In the Customize Default Entity Generation page, select java.util.List for the collection properties type and enter model as the package name. The new entity will be added to the HRAppModel project under the model package. Click Next to continue.

  14. alt text
  15. In the Tables and columns area, select and then expand EMPLOYEES. Notice that the proposed class name is Employee.

  16. alt text
  17. Select any column to see the column properties. Notice that you can change the properties that will be generated for the column. Click Finish to generate the entities.

  18. alt text
  19. When the OEPE JPA Entity Generation Wizard finishes, the generated JPA entity classes are added to your project's persistence.xml file. Expand HRAppModel > src > META-INF and double-click persistence.xml. Open the Managed Classes accordion to view the generated classes.

  20. alt text
  21. In the Project Explorer, expand HRAppModel > src > model to see the generated classes (Department.java, Employee.java, and Location.java).

  22. alt text
  23. Double-click Department.java to open the class in the editor. Review the generated code.

  24. alt text

    Notice the metadata annotations used: @Entity, @Table, and so forth. Show more or lessRead more... The Java Enterprise Edition (JEE) Enterprise JavaBeans (EJB) are a component architecture that you use to develop and deploy object-oriented, distributed, enterprise-scale applications. An application written according to the Enterprise JavaBeans architecture is scalable, transactional, and secure.

    The EJB 3.0 Java Persistence API (JPA) improves the EJB architecture by reducing its complexity through the use of metadata (annotations) and specifying programmatic defaults of that metadata.

    EJB 3.0 and the Java Persistence API rely heavily on metadata annotation, a feature that was introduced in J2SE 5.0.

    In the next few steps, you add named query elements to the entity classes that were generated by the wizard. These named queries will eventually provide client applications access to the data managed by the class. You use Java Persistence Query Language (JPQL) to annotate the class with named queries that retrieve all the rows of the tables. Later, you add a specific filtered query to one of the classes.

  25. Insert a new line in the Department.java source file just below the @Table annotation. On that line, start typing @NamedQu. Click the red x in the margin and select Change to 'NamedQuery'...  from the pop-up list.

  26. alt text
  27. Complete your statement with the following code:
    @NamedQuery(name="Department.findAll", query="select d from Department d "). Your new Java code should be:

  28. alt text

    When defining the JPQL query string, OEPE provides content assist and validation against the active database connection.

    In the next few steps, you add named queries to the Employee.java and Location.java source files.

  29. Double-click Employee.java to open it in the editor.

  30. alt text
  31. Insert a new line in the Employee.java source file just below the @Table annotation. Just like you did in earlier steps, start typing @NamedQu. Click the red x in the margin and select Change to 'NamedQuery'...  from the pop-up list.

  32. alt text
  33. Complete your statement with the following code:
    @NamedQuery(name="Employee.findAll", query="select e from Employee e"). Your new Java code should be:

  34. alt text
  35. Double-click Location.java to open it in the editor.

  36. alt text
  37. Insert a new line in the Location.java source file just below the @Table annotation. On that line add a @ NamedQuery annotation.

  38. alt text
  39. Complete your statement with the following code:
    @NamedQuery(name="Location.findAll", query="select l from Location l "). Your new Java code should be:

  40. alt text

You have now completed creating the JPA entities for your application model. Before we move on to creating the session bean, we'll look at the entities you created.

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. In the Project Explorer, right-click HRAppModel and select JPA Tools > Show in Entity Editor.

  2. alt text
  3. Notice that the diagram shows all the entities, attributes, and relationships in your project.

  4. alt text
    Modeling EJB/JPA components on a diagram can be useful for the developer Show more or lessRead more...
  5. Using the Entity diagram, you can create new entities from tables and create new entities from Java files. You can also use the diagrammer to customize how your diagram appears. For example you can hide one-to-one relationships.

  6. alt text

    In the next section you create a session bean to provide access to your JPA entity model.

Step 4: Create a Session Bean
A Session Facade presents client objects with a unified interface to the underlying EJBs. The client interacts only with the facade, which resides on the server and invokes the appropriate EJB methods. Show more or lessRead more...

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.0 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. If the interface of an EJB changes, client objects have to be changed as well.

In this section, you create a session bean that implements the methods defined in the Employee, Department, and Location entities.

  1. In the Project Explorer, right-click HRAppModel and select New > Session Bean (EJB 3.x) from JPA Entities.

  2. alt text
  3. In the wizard, enter oracle.service as the Java package, and HRAppFacade as the class name. Click Next.

  4. alt text
  5. Select all three entities and click the shuttle button to shuttle them to the Selected entities area.

  6. alt text
  7. Click Finish to create the session bean.

  8. alt text
  9. The wizard creates the following session bean (HRAppFacade.java):

  10. alt text
Step 5: Create ADF Data Controls

You now expose the EJB as an Oracle ADF data control. ADF data controls simplify the way that you bind user interfaces to the EJB. To learn more about Oracle ADF, visit: http://oracle.com/technology/products/adf. To create ADF data controls from EJB session beans, perform the following steps:

  1. In the Project Explorer, expand HRAppModel > src > oracle.service. Right-click HRAppFacade.java and select Model Components > Create ADF Data Control.

  2. alt text
  3. The wizard creates the ADF data control, which is defined in a set of .xml files stored in the adfmsrc package.

  4. alt text

You have now created the model project that contains the JPA entities, the Session Facade (which publishes access to client applications), and the ADF data controls. In the next section of this tutorial, you build a web client application that consumes the components that you just created.

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