Build A JPA Application
Build A JPA Application
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.
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...]
1. 2. 3. 4. 5. 6.
Download the schema zip file from OTN. If you created the schema previously, go to the next cue card. 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
Port number for your database, for example, 1521 Database System Identifier, for example, XE or ORCL Database user with admin privileges, for example,
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.
In the IDE
After running the Ant task, you should see a build successful message in the JDeveloper Log window. [ tell me more...]
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...]
1. Open the Create Application wizard [ File > New > General > Applications] . 2. To follow along with the example, enter the application name ProductsApplication. 3. Select Generic Application in the Applica tion 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
When you complete the steps for creating a new application and project, the Projects panel in the Application Navigator should look like this: [ tell me more...]
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...]
1. Open the Create Database Connection dialog [ File > New > General > 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. 4. Click Test Connection to confirm that you can connect. 5. Click OK if the connection was successful. 6. 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...]
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. [ tell me more...]
1. In the Application Navigator, select the project you created and launch the Create Entities from Tables wizard. [ File > New > Business Tier > EJB > Entities from Tables] 2. On step 1 of the wizard, Select EJB Version, select EJB 3.0 -- JPA Entities . Click Next . 3. On step 2, Persistence Unit, do not create a new unit. Click Next . 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 A vailable list. From the list, click PRODUCTS_BASE, then shuttle it to the Selec ted 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...]
7. 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 : Fiel ds is selected Implement java.io. Serializable is selected java.util.List is selected on the Collection Type for Relationship Fields dropdown list 8. 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...]
9. Click
In the IDE
When you complete the steps for creating entities from tables, JDeveloper displays messages in the EJB Log window: [ tell me more...]
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...]
1. In the Application Navigator, expand Application Sources | META-INF and select persistence.xml, then launch the Create Java Service Facade wizard [ File > New > Business Tier > EJB > Java Service Facade] . 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. 3. Confirm that Generate a main() method with sample client code is selected, then 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...]
4. On step 2, Create Persistence Unit, enter the persistence unit name Project1Outside. Confirm that JDBC Connection is selected and the database connection you created is selected on the dropdown list (for example, FOD). Then click Next .
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...]
5. 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...]
6. Click
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...]
To create a runnable Java service facade, you will add implementation code to the main() method. [ tell me more...]
1. If it is not already open, double-click ProductsJavaServiceFacade.java in the Application Navigator to open it in the source editor.
2. In the source editor, add code that will print out a list of product names. Use sample code 3. In the Application Navigator, right-click ProductsJavaServiceFacade.java and choose R un. You should see a list of product names in the log window.
In the IDE
When you click R un , 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: [ tell me more...]