You will use download the schema zip file and install it. The tutorial use tables that are part of the Fusion Order Demo (FOD) schema. Then use wizards to quickly create an application and a project, and to connect to the database.
show more or lessRead more...

You will use wizards to create JPA entities from existing database tables and a Java service facade that has a main() method. Then you will generate a persistence unit that is configured to run outside the Java EE container. In the source editor, you will add code to the main() method, and then run the Java service facade. Then the Running Log window opens, displaying the result of the execution of the class running outside the Java EE container, and returning a list of product names:

Running Log window

Purpose Duration Application
This tutorial shows you how to build a JPA application that uses JPA entities and a simple Java service facade that runs outside the Java EE container. 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. 30 minutes Download productsapplication.zip
Note: If you download the complete application, Show more or lessRead more... do the following before you run the application:
  • Follow all the instructions in Step 1: Install the Schema.

  • Open the application in JDeveloper. In the Application Navigator, expand the Application Resources panel. Then expand Connections > Database. Right-click FOD and choose Properties. Modify only the following properties so that they point to the database where you installed the FOD schema: Host Name, Port, SID. Then click Test Connection to confirm that you can connect. Click OK if the connection was successful.

  • To run the application, follow the instruction in Step 6, substep #3.

Step 1: Install the Schema
    The steps and examples in the tutorial are based on tables that are part of the Fusion Order Demo (FOD) schema. This schema is also used as the database for the sample application that ships with Oracle JDeveloper 11g, as well as other collateral in this release. Show more or lessRead more...

    It will be convenient to have this schema installed, and you only need to do it once. If you have already installed the FOD schema, you can skip this step and go directly to the next. To install the schema, you will also need to have an Oracle database and access to a privileged (DBA) user which has permissions to create users. Any Oracle 10g version (or later) of the database should work for this schema. To install the schema, you will download and expand a zip file, then use Ant to perform the installation.
  1. Download the schema.zip file from OTN. Download schema.zip

  2. Unzip the file to a convenient location (for example, c:\temp).

  3. From the File menu, choose Open. Open the workspace c:\temp\Infrastructure\Infrastructure.jws.

  4. If you are prompted to migrate the project, click Yes to confirm, then click OK.

  5. In the Application Navigator, expand the MasterBuildScript project. Under Resources double-click build.properties to open it in the editor.

  6. Set the appropriate values in the build.properties file (jdeveloper.home, and all the jdbc.* and db.* settings). Keep all other properties set to their default values. The demo user must be named FOD.

    The relevant settings are:

    Setting Description
    jdeveloper.home The /jdeveloper directory where you have JDeveloper installed, for example, c:/JDeveloper_11/jdeveloper/
    jdbc.urlBase Base URL for your database, for example, jdbc:oracle:thin:@localhost
    jdbc.port Port number for your database, for example, 1521
    jdbc.sid Database System Identifier, for example, XE or ORCL
    db.adminUser Database user with admin privileges, for example, system
    db.demoUser.tablespace Tablespace name where the FOD user will be installed, for example, USERS
  7. In the MasterBuildScript project, select the build.xml file.

  8. In the Structure window, right-click the refreshSchema target and choose Run Target "refreshSchema".

  9. When prompted, enter the password for the database user with admin privileges. After running the Ant task, you should see a build successful message in the JDeveloper Log window.

    Apache Ant log window

    If there are errors, you should verify that your database is running (try connecting through SQL*Plus), and that the values you provided in the build.properties file are correct. Also, make sure that the DBA user you specified has the CREATE USER privilege.

Step 2: Create a New Application and Project
  1. From the main menu, choose File > New. In the New Gallery, expand the General category and select Applications. Then in the Items list, select Custom Application and click OK.

    New Gallery, Applications
  2. The JDeveloper application is the highest level in the organizational structure. While you are developing your application, it stores information about the objects you are working with. Show more or lessRead more... At the same time, it keeps track of your projects and all environment settings.

    Based on prebuilt templates, a JDeveloper application allows you to specify a predefined type of environment, depending on the type of application you want to create (web application, Java application, and so on). Application templates provide you with a quick way to create the project structure for standard applications with the appropriate combination of features already specified. The application template also filters the work you do in JDeveloper such that the choices available are focused only on the features you are working with.

    In this tutorial, you will use the Custom Application template, which makes available objects associated with all the features that JDeveloper supports in a single project.

    Once you have created an application using a suitable template, you can still add new projects to the application and specify what features are to be included. To do this, in the Application Navigator, right-click the application name and choose New Project. In the New Gallery, you can select any type of project in the Items list.
  3. To follow along with the example, enter ProductsApplication as the application name.

    New Application Dialog
  4. The application template you select determines the initial project structure, that is, the named project folders within the application workspace, and the application libraries that will be added. Show more or lessRead more... The project or projects in the application define the associated features.

    A JDeveloper project, which is used to logically group files that are related, keeps track of the source files, packages, classes, images, and other elements that your program may need. Projects manage environment variables such as the source and output paths used for compiling and running your program. Projects also maintain compiler, runtime, and debugging options so that you can customize the behavior of those tools per project.

    You can add multiple projects to your application to easily access, modify, and reuse your source code. Different projects might contain files representing different tiers of a multi-tier application, for instance, or different subsystems of a complex application. These files can reside in any directory and still be contained within a single project.
  5. Accept the defaults and click Finish.

    The Projects panel in the Application Navigator should look like this:

    Application Navigator

  6. A new application created from a template appears in the Application Navigator already partitioned into tiered projects, with the associated features set in each project. Show more or lessRead more... Projects are displayed as the top level in the hierarchy in the Application Navigator. The Custom Application template that you used for your application creates one project using a default project name (or the project name you entered).

    In the Application Navigator you can collapse and expand any panel. You adjust the size of panels by dragging the splitter between two panels. To group and sort items in the Projects panel, use the navigator display options icon Navigator Display Options dropdown menu. For application operations, you can click application icon Application Menu and choose an option from the dropdown menu.

    JDeveloper has the capability of recognizing many different file types, displaying each in its appropriate viewer or editor when you double-click the file in the Application Navigator. Closing an application or project closes all open editors or viewers for files in that application or project and unloads the files from memory.

    Note: Nodes in italics in the Application Navigator mean that the elements have not yet been saved. A project node is bold when a file in the project is selected.

    From the main menu, choose Application > Show Overview. The Application Overview window opens in the editor window area.

    Part of Application Overview window

    All objects that you create within JDeveloper appear in the Application Overview file summary pages, arranged by object type. As you create new files and artifacts, you can view them filtered by status and project.

    You can optionally close the Application Overview window, since you will not be using it to create objects for this application.
Step 3: Create a Database Connection

You will establish a connection to the database you installed, to use the Fusion Order Demo (FOD) schema.

  1. From the main menu, choose File > New > General > Connections > Database Connection, then click OK to open the Create Database Connection dialog.

    New Gallery, Connections, Database Connection
  2. Select Application Resources if it is not selected as the Create Connection In option.

  3. Enter a name for the connection, select the connection type, and enter the username and password. To follow along with this example, use FOD for the connection name and fod for the username.

    Create Database Connection dialog
  4. Enter the Oracle JDBC settings to point to the host, port and SID for your database.

    Create Database Connection dialog, bottom
  5. Click Test Connection to confirm that you can connect. Click OK if the connection was successful.

  6. In the Application Navigator, expand the Application Resources panel, then expand the Connections and Database nodes to see the database objects.

    Application Navigator, Application Resources panel
  7. The database connection you created (for example, FOD) is included as a resource for your application under the Connections | Database node. Show more or lessRead more...

    Expand the connection node you created, then click the various + icons to see the schema and database objects.

    Application Resources panel, database and schema objects

    After adding the database connection to the application resources, you can optionally contract the Application Resources panel to increase the size of the Projects panel in the Application Navigator.
Step 4: Create JPA Entities from Tables

The Enterprise JavaBeans (EJB) 3.0 specification includes an additional persistence specification called the Java Persistence API (JPA), which is an API for creating, removing and querying across lightweight Java objects. The EJB 3.0 specification no longer refers to entities as "beans", they are now JPA entities.

To begin creating JPA entities, you will launch the Create Entities from Tables wizard.

    The Java Persistence API (JPA) greatly simplifies Java persistence. It provides an object-relational mapping approach that allows you to declaratively define how to map Java objects to relational database tables in a standard, portable way that works both inside a Java EE 5 application server and outside an EJB container in a Java Standard Edition (Java SE) 5 application. Show more or lessRead more...

    A JPA entity application can be packaged in a persistence archive, which is a JAR file that must have a JPA persistence descriptor file, persistence.xml. One or more persistence units are configured in the persistence.xml file. Each persistence unit must have a name. A named persistence unit provides a convenient way to specify a set of metadata files, and classes, and jars that contain all the classes to be persisted in a grouping.

    When you use the Create Entities from Tables wizard to create the JPA entity, JDeveloper automatically creates a default persistence unit. In the Application Navigator, expand Application Sources, then expand META-INF. Double-click persistence.xml to open the file in the overview editor. If necessary, click Overview to switch to the overview editor. Use the overview editor for JPA persistence when you want to create new persistence units, and delete or edit existing ones in the persistence.xml file.

    Overview editor for the persistence.xml file

    The default persistence unit generated by JDeveloper has the name Project1 in the persistence.xml file. Note that the default name of the persistence unit is derived from the project name. In the example, you will not be using the default Project1 persistence unit. Instead you will be creating a new persistence unit that is configured to run outside the Java EE container, which you will do in the next set of steps.
  1. In the Application Navigator, right-click the project you created and choose New > Business Tier > EJB > Entities from Tables, then click OK to launch the Create Entities from Tables wizard.

    New Gallery, EJB, Entities from Tables
  2. On step 1 of the wizard, Version, select EJB 3.0 -- JPA Entities. Click Next.

    Create Entities from Tables wizard
  3. On step 2, Persistence Unit, do not create a new unit. Click Next.

    Persistence Unit page of wizard
  4. On step 3, Type of Connection, select Online Database Connection. Click Next.

  5. On step 4, Database Connection Details, make sure FOD is selected on the Connection dropdown list. Click Next.

  6. On step 5, Select Tables, click Query to populate the Available list. From the list, click PRODUCTS_BASE, then blue right arrow shuttle it to the Selected list. Click Next.

    Select Tables page of wizard
  7. On step 6, General Options, accept the default package name. Under Entity Class Options, make sure the following options are set, then click Next:

    Option Value
    Fields selected
    Implement java.io.Serializable Interface selected
    Generate @NamedQuery Annotation to Retrieve All Instances selected
    General Options page of wizard
  8. On step 7, Specify Entity Details, accept the default entity name and class for the table FOD.PRODUCTS_BASE. Click Finish.

    Specify Entity Details page of wizard
  9. Click the save all icon Save All to save your work.

    When you complete the steps for creating entities from tables, JDeveloper displays messages in the EJB Log window:

    EJB log window
  10. The Application Navigator should look similar to the following when all the folders in the project are expanded: Show more or lessRead more...

    Application Navigator

    The project now contains these components:
    • persistence.xml: A metadata file that declares that a JAR file is a JPA persistence archive. In the metadata, one or more persistence units may be defined. Select persistence.xml in the Application Navigator. In the Structure window, notice the default persistence unit that was automatically generated by JDeveloper.

    • ProductsBase.java: The EJB-JPA entity that represents the PRODUCTS_BASE table in the FOD schema.

Step 5: Generate a Java Service Facade and Persistence Unit
    A persistence unit identifies a subset (or possibly all) of the entities included in a deployed project (an EJB JAR or WAR file). Show more or lessRead more... The persistence unit associates metadata with this entity group, including whether it runs inside or outside a Java EE container. A Java service facade is a Java class that wraps JPA entities and provides query and transaction methods. In the IDE you can quickly generate a Java service facade by using the Create Java Service Facade wizard. In the same wizard, you can also create a new persistence unit that is configured to run outside of a container.
  1. In the Application Navigator, expand Application Sources | META-INF and right-click persistence.xml, then choose New Java Service Facade to launch the Create Java Service Facade wizard.

    New Java Service Facade context menu item
  2. On step 1 of the wizard, Java Service Class, to ensure you don't overwrite any changes if you generate another Java service facade, rename the Java service class to project1.ProductsJavaServiceFacade.

    Create Java Service Facade wizard
  3. Deselect Generate a Sample Java Client. Then click Next.

    Unlike a session facade, a Java service facade does not run inside the container, so you do not need to create another sample client to call methods. You can use the Java service facade as its own client by adding a main() method in the Java service class.

  4. On step 2, Create Persistence Unit, enter the persistence unit name Project1-Outside. Confirm that JDBC Connection is selected and the database connection you created is selected on the dropdown list (for example, FOD). Then click Next.

    Create Persistence Unit page of the wizard
  5. In this step, you will create a persistence unit that runs outside of a container for the same entity. Show more or lessRead more... In the previous step, when you used the Create Entities from Tables wizard to create the JPA entity, JDeveloper automatically created a default persistence unit Project1 in the persistence.xml file. That default persistence unit is configured to run inside a Java EE container.

    The same entity can appear in multiple persistence units, but hold different mapping behavior in each one. In addition to defining the entity classes that it represents, a persistence unit also defines its data source information (when overriding the default connection) and any of a number of platform-specific properties. A common need for creating separate persistence units is to run the same entity or entities both inside and outside the Java EE container.
  6. On step 3, Java Service Facade Methods, expand both nodes to make sure all methods have been selected. Then click Finish.

    Java service facade methods
  7. The methods beneath an entity are named queries, as well as methods for creating and updating entities. Show more or lessRead more... The getEntitynameFindAll() method and removeEntityname() method (where Entityname is the name of the entity) are generated by default for each entity. If you had created additional named queries on an entity, they would appear in this list as well.
  8. Click the save all icon Save All to save your work.

    When you complete the steps for creating the persistence unit and Java service facade, the IDE should look similar to this:

    Application Navigator, Java source editor

    JDeveloper opens the ProductsJavaServiceFacade.java file in the source editor. The Java service class contains the methods you have selected to be exposed in the facade.

  9. In the Application Navigator, if you double-click persistence.xml to open the file in the overview editor, you should see two persistence units: Show more or lessRead more...

    Overview editor for the persistence.xml file

    When you click the Source editor tab to switch to the XML editor, you should see the code JDeveloper generated for you when you created the new persistence unit that is configured to run outside a container:
Step 6: Run the Java Service Facade
  1. If it is not already open, double-click javanode icon ProductsJavaServiceFacade.java in the Application Navigator to open it in the source editor. To create a runnable Java service facade, you will add a main() method.

    Application Navigator
  2. In the source editor, add a main() method that will print out a list of product names, after the public method:

    public ProductsJavaServiceFacade(){...}

    Copy and paste the following code:

  3. In the Application Navigator, right-click javanode icon ProductsJavaServiceFacade.java and choose Run.

    When you click Run, JDeveloper first saves and compiles the project. The details of the compile are shown in the Messages Log window. Then the Running Log window opens, displaying the result of the execution of the class running outside the Java EE container, and returning a list of product names:

    Project running log wiindow showing output

Summary
In this tutorial you built a JPA application that uses JPA entities and a simple Java service facade that runs outside the Java EE container. You learned how to: To learn more about developing Java EE applications and JDeveloper, refer to:


Bookmark Print Expand all | Hide all
Back to top

Did you find this page helpful?

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