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:
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 |
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.
Download the schema.zip file from OTN.
-
Unzip the file to a convenient location (for example,
c:\temp
). -
From the File menu, choose Open. Open the workspace
c:\temp\Infrastructure\Infrastructure.jws
. -
If you are prompted to migrate the project, click Yes to confirm, then click OK.
-
In the Application Navigator, expand the MasterBuildScript project. Under Resources double-click build.properties to open it in the editor.
-
Set the appropriate values in the build.properties file (
jdeveloper.home
, and all thejdbc.*
anddb.*
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
orORCL
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
-
In the MasterBuildScript project, select the build.xml file.
-
In the Structure window, right-click the refreshSchema target and choose Run Target "refreshSchema".
-
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.
If there are errors, you should verify that your database is running (try connecting through SQL*Plus), and that the values you provided in thebuild.properties
file are correct. Also, make sure that the DBA user you specified has the CREATE USER privilege.
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.
-
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.
-
To follow along with the example, enter
ProductsApplication
as the application name. Accept the defaults and click Finish.
The Projects panel in the Application Navigator should look like this:
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.
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.
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 dropdown menu. For application operations, you can click 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.
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.
You will establish a connection to the database you installed, to use the Fusion Order Demo (FOD) schema.
-
From the main menu, choose File > New > General > Connections > Database Connection, then click OK to open the Create Database Connection dialog.
-
Select Application Resources if it is not selected as the Create Connection In option.
-
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 andfod
for the username. Enter the Oracle JDBC settings to point to the host, port and SID for your database.
-
Click Test Connection to confirm that you can connect. Click OK if the connection was successful.
-
In the Application Navigator, expand the Application Resources panel, then expand the Connections and Database nodes to see the database objects.
Expand the connection node you created, then click the various + icons to see the schema and database 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.
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.
-
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.
-
On step 1 of the wizard, Version, select EJB 3.0 -- JPA Entities. Click Next.
-
On step 2, Persistence Unit, do not create a new unit. Click Next.
-
On step 3, Type of Connection, select Online Database Connection. Click Next.
-
On step 4, Database Connection Details, make sure FOD is selected on the Connection dropdown list. Click Next.
-
On step 5, Select Tables, click Query to populate the Available list. From the list, click PRODUCTS_BASE, then shuttle it to the Selected list. Click Next.
-
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 -
On step 7, Specify Entity Details, accept the default entity name and class for the table FOD.PRODUCTS_BASE. Click Finish.
-
Click Save All to save your work.
When you complete the steps for creating entities from tables, JDeveloper displays messages in the EJB Log window: 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.
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.
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.
The project now contains these components:
-
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.
-
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
. -
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 amain()
method in the Java service class. -
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. -
On step 3, Java Service Facade Methods, expand both nodes to make sure all methods have been selected. Then click Finish.
-
Click 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:
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.
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.
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.
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:
<provider>
org.eclipse.persistence.jpa.PersistenceProvider
</provider>
<class>
project1.ProductsBase
</class>
<properties>
...
</properties>
</persistence-unit>
-
If it is not already open, double-click 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. -
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: -
In the Application Navigator, right-click 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:
final ProductsJavaServiceFacade productsJavaServiceFacade = new ProductsJavaServiceFacade();
List<ProductsBase> prodList = productsJavaServiceFacade.getProductsBaseFindAll();
for (ProductsBase prod : prodList) {
System.out.println(prod.getProductName());
}
}
- Use JDeveloper wizards and dialogs to create an application and database connection
- Generate a persistence unit that is configured to run outside the Java EE container
- Use the source editor to add code to the
main()
method - Run the Java service facade
- Creating a Simple EJB Session Bean
- Getting Started with the JDeveloper IDE
- User's Guide for Oracle JDeveloper
- Java EE Developer's Guide for Oracle ADF