You can work in JDeveloper's
IDE to create a JPA application that uses JPA entities and a simple Java service facade that runs outside the Java EE container.
Install the Schema: Download the schema zip file from OTN and install it. The cue cards use tables that are part of the Fusion Order Demo (FOD) schema.
Create a New Application: Use the Create Application wizard to create a generic application.
Create a Database Connection: Use the Create Database Connection dialog to create a connection to the schema.
Create JPA Entities: Use the Create Entities from Tables wizard to create JPA entities from existing database tables.
Generate a Java Service Facade: Use the Create Java Service Facade wizard to create a Java service facade that has a main() method.
Generate a Persistence Unit: Use the Create Java Service Facade wizard to generate a persistence unit that is configured to run outside the Java EE container.
Add Code and Run the Java Service Facade: Add code to the main() method and run the Java service facade.
Install the Schema
Installing the Schema
The steps and examples in the cue cards 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. 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 card. [tell me more...]
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 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:
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
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.
In the IDE
After running the Ant task, you should see a build
successful
message in the JDeveloper Log window. [tell me more...]
Create a New Application and Project
Creating a New Application and Project
The JDeveloper application is the highest level in the organizational structure. It stores information about the objects you are working with, while you are creating your application. It keeps track of your projects and the environment settings.
[tell me more...]
Open the Create Application wizard [File > New > General > Applications] .
To follow along with the example, enter the application name ProductsApplication.
Select Generic Application in the Application Template list, and click Finish.
Application Templates
Application templates provide you with a quick way to create the project structure for standard applications with the appropriate combination of technologies already specified. The new application created from the template appears in the Application Navigator already partitioned into tiered projects, with the associated technology scopes set in each project. [tell me more...]
In the IDE
The Application Overview window opens by default in the editor window area when you create a new application. All objects that you create within JDeveloper appear in the Application Overview, arranged by object type. You can create new objects, read about new types of objects that you haven't yet created, and initiate actions from the objects' context menus. [tell me more...]
Create a Database Connection
Creating a Database Connection
You can connect to any database for which you have connection details, or install the sample schema used in the cue card examples and then establish a connection to it. If you installed the sample schema, you will be able to follow the steps in the cue cards exactly as written. If you work with your own database, you can supply your own values as needed. [tell me more...]
Open the Create Database Connection dialog [File > New > General > Connections > Database Connection] .
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 and fod for the username.
Click Test Connection to confirm that you can connect.
Click OK if the connection was successful.
In the Application Navigator, expand the Connections and Database nodes in the Application Resources panel to see the database objects.
In the IDE
When you complete the steps for creating a database connection, the Application Navigator should look similar to this when you expand the Application Resources panel. The database connection is now included as a resource for your application under the Connections | Database nodes. [tell me more...]
Create JPA Entities from Tables
Creating 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.
In the Application Navigator, select the project you created and launch the Entities from Tables wizard. [File > New > Business Tier > EJB > Entities from Tables]
On step 1 of the wizard, Select EJB 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 click to move it to the Selected list. Click Next.
Creating JPA Entities
By using the Create Entities from Tables wizard, you can create more than one entity at the same time. In the example, you will select one table in the FOD schema to create the JPA entity. [tell me more...]
On step 6, General Options, accept the default package name. Under Entity Class Options, make sure you have the following options set, then click Next: Fields is selected Implement java.io.Serializable is selected java.util.List is selected on the Collection Type for Relationship Fields dropdown list
On step 7, Specify Entity Details, accept the default entity name and class for the table FOD.PRODUCTS_BASE. Click Finish.
JPA Persistence
The Java Persistence API (JPA), part of the Java Enterprise Edition 5 (Java EE 5) Enterprise Java Beans (EJB) 3.0 specification, 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. [tell me more...]
Click Save All to save your work.
In the IDE
When you complete the steps for creating entities from tables, JDeveloper displays messages in the EJB log window: [tell me more...]
Generate a Java Service Facade and Persistence Unit
Generating a Persistence Unit and Java Service Facade
A persistence unit identifies a subset (or possibly all) of the entities included in a deployed project (an EJB JAR or WAR file). The persistence unit associates metadata with this entity group, including whether it runs inside or outside a Java EE container. [tell me more...]
In the Application Navigator, under Application Sources | META-INF, select persistence.xml and launch the Create Java Service Facade wizard [File > New > Business Tier > EJB > Java Service Facade] .
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.
Select Generate a main() method with sample client code. Click Next.
Generate a main() Method
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. [tell me more...]
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 shown in the dropdown list (for example, FOD). Then click Next.
Creating Persistence Units
In the previous cue card, 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. [tell me more...]
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
When you use the wizard to create a Java service facade, you can choose to generate Java service facade methods for every entity in the selected persistence unit (if there is more than one entity). [tell me more...]
Click Save All to save your work.
In the IDE
When you complete the steps for creating the persistence unit and Java service facade, the IDE should look similar to this: [tell me more...]
Run the Java Service Facade
Running the Java Service Facade
To create a runnable Java service facade, you will add implementation code to the main() method. [tell me more...]
If it is not already open, double-click ProductsJavaServiceFacade.java in the Application Navigator to open it in the Java source editor.
In the source editor, add code that will print out a list of product names. Use sample code
Click Save to save the file changes.
In the Application Navigator, right-click ProductsJavaServiceFacade.java and choose Run. You should see a list of product names in the log window.
In the IDE
After you have added the sample code to the main() method, the Java source editor should look similar to this: [tell me more...]