Build a Web Application with EJB, JPA
and JSF - Part 1: Build the Data Model
You can easily install the schema and connect to the database,
and then create an application and projects in which
to organize your application files. Then work in JDeveloper's
IDE to create the business services for your application.
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 and Project: Use
the Create Application wizard to create an application
and projects for the EJB Components model and the
user interface portions of the application.
Create a Database Connection: Use the Create Database Connection dialog to create a connection to the schema.
Create Entities from Tables: Use the Create Entities from Tables wizard to create JPA entities.
Create an EJB Diagram: Use the Create
EJB Diagram dialog then drag and drop tables onto the diagram.
Create a Session Bean: Use the Component Palette to launch the Create Session Bean wizard.
Add a Named Query: Double-click the entity
in the diagram to open it in the source editor and add
sample code.
Create and Run a Sample Java Client: Use
the Create Sample Java Client dialog to create the file,
and then edit it in the source editor.
Run the Java Service Outside the Java EE Container: Use
the Create Persistence Unit dialog opened from the Create Java Service Facade wizard to create a persistence
unit that runs outside Java EE container.
Expose the EJB as a Data Control: In
the Application Navigator, create the data control from
FODFacadeBean.java.
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 while you
are developing. [tell me more...]
Open the Create Application wizard [File > New > General > Applications > Application] .
Enter the application name OrdersApplication to follow along with the example.
In the Application Package Prefix field,
enter oracle.
Select Java EE Web Application from
the Application
Template list, and click Next.
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...]
On step 2, enter UserInterface as the
name for the view and controller project, and click Next
twice.
On step 4, enter EJBModel as the
name for the model project, and click Finish.
In the Application Navigator, expand the Application
Resources panel to see where resources for this application are displayed.
In the IDE
In the Application Navigator, projects are displayed as
the top level in the hierarchy. The
Application Navigator should now look like this: [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
The database connection is now included as a resource
for your application. [tell me more...]
Create Entities from Tables
Creating Entities from Tables
Enterprise JavaBeans (EJB) technology is the server-side component architecture for Java Platform, Enterprise Edition (Java EE) that encapsulates business logic. EJB technology enables rapid and simplified development of distributed, transactional, secure and portable applications based on Java technology. [tell me more...]
In the Application Navigator, select the EJBModel project you just created and launch the Create Entities from Tables wizard [File > New > Business Tier > EJB > Entities from Tables] .
On step 2, click Next to
let the wizard create a default persistence unit definition.
On step 3, confirm that Online Database
Connection is selected and
click Next.
On step 4, confirm that FOD is
the selected database connection and click Next.
Connecting to a Database
The database connection you made earlier is available
as an application resource, and can be selected from the
Connection dropdown list, if it is not selected
by default. [tell me more...]
On step 5, click Query to
populate the Available list.
Querying the Database and Selecting Tables
In the wizard page, you can click Query to
query the database and list all the available tables. From
the list of tables, you can select the tables you want
to work with in your application. In this example, you
will select the ORDERS and ORDER_ITEMS tables. [tell me more...]
Select two tables, ORDERS and ORDER_ITEMS
from the Available list and shuttle them to the Selected list.
Then click Next.
On step 6, accept the default package name. Make sure the following are selected under Entity Class Options, then click
Next:
Fields Implement java.io.Serializable Collection Type for Relationship Fields: java.util.List
On step 7, accept the default values and click Finish.
Click Save All to save your work.
In the IDE
In the Application Navigator, one Java file is created for Orders
and one for OrderItems, in the Application
Sources node. These are the JPA entities that were created
from the Orders
and Order_Items tables. [tell me more...]
Create an EJB Diagram
Creating an EJB Diagram
You can model Enterprise JavaBeans on a
diagram to visualize a design as you develop it. Whenever
a bean is modeled, the underlying implementation files
are also created. [tell me more...]
In the Application Navigator, select the EJBModel project
and open
the Create EJB Diagram dialog [File > New > Business Tier > EJB > EJB Diagram] .
Enter EJB Diagram as the diagram name, and accept the default package name. Then click OK.
Click OK to associate the diagram
with the EJBModel (EJBModel.jpr) persistence unit.
Adding
Objects to the Diagram
The EJB diagram created with the dialog is opened in the
editor area. [tell me more...]
In the Application Navigator, select the Orders and OrderItems entities,
then drag and drop them onto the EJB diagram.
Click Save All to save your work.
In the IDE
The diagram displays the EJB components that correspond
to the reverse engineering action you performed on Orders
and OrderItems. The Orders and OrderItems entities are displayed
along with a representation of their relationship. [tell me more...]
Create a Session Bean
Creating 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. 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, so that if the interface of an EJB changes, client
objects have to be changed as well. [tell me more...]
In the Component Palette, EJB Components page, EJB Nodes panel, select the Session Bean component
then drag and drop it onto the diagram.
In the Create Session Bean wizard that launches, enter FODFacade as
the EJB name and click Next.
On step 2 of the wizard, confirm that the following default options
are set: Session Type: Stateless Transaction Type: Container Generate Session Facade Methods: selected Entity Implementation: JPA Entities Persistence Unit: EJBModel (EJBModel.jpr)
Click Next.
On step 3, expand the Orders and OrderItems nodes,
and deselect the Orders.findAllByRange and
OrderItems.findAllByRange entity
methods. Then click Next.
On step 4, accept
the default name
for the bean class, and click Next.
On step 5, confirm that both Implement
a Remote Interface and Implement
a Local Interface are selected. Accept the default names of the interfaces. Then click Next.
Using a Local or Remote Interface
Clients access an EJB component through its interface.
The remote interface is used for client applications that
run in a separate virtual machine, such as Java clients.
The local interface is used for client applications that
run in the same virtual machine, such as web clients. [tell me more...]
Review the summary of created classes, then click Finish.
In the IDE
The EJB diagram now includes the session bean you just created. [tell me more...]
Add a Named Query
Adding a Named Query
Named queries enable you to define queries at design time
and then use them at run time. A NamedQuery metadata statement
has been created by default in the OrderItems.java entity,
by the Create Entities from Tables wizard. It retrieves
all rows from the OrderItems table: [tell me more...]
In the EJB diagram, double-click the OrderItems entity
bean to open it in the source editor.
Editing
Source Code
Features available to you in the source editor include: [tell me more...]
Add a query to the class that retrieves order items by quantity. Use sample code
Click Make to
compile the OrderItems.java class.
To add the new method to the session bean, in the
Application Navigator, right-click the FODFacadeBean.java node
and choose Edit
Session Facade.
In the Specify Session Facade Options dialog, expand
the OrderItems node and select
OrderItems.findByQuantity. Then
click OK.
In the IDE
In the Specify Session Facade Options dialog, you can expose
the new method by selecting it from the available nodes. All
entities in this project appear as nodes in the tree. [tell me more...]
Create and Run a Sample Java Client
Creating and Running a Sample Java Client
JDeveloper includes a sample Java client utility you can
use to test an EJB. To generate a sample Java
client, you will right-click a session bean in
the Application Navigator and choose New
Sample Java Client. [tell me more...]
In the Application Navigator, right-click FODFacadeBean.java and choose New
Sample Java Client.
Confirm that the client class name is oracle.model.FODFacadeClient.
Make sure IntegratedWLSConnection is selected in the Application Server Connection dropdown list, then
click OK.
In the FODFacadeClient class,
add a value parameter 3 to the FindByQuantity method:
(List<OrderItems>)fODFacade.queryOrderItemsFindByQuantity(3)).
Click Save All to save your work.
In the Application Navigator, right-click FODFacadeBean.java and
choose Run to launch the facade bean in the Integrated WLS server instance.
Once the default server instance has started, right-click FODFacadeClient.java and
choose Run.
Running the Sample Client
To test your business services, you will right-click the
session bean in the Application Navigator and choose Run. JDeveloper automatically starts the DefaultServer instance, if it is not already running. Then JDeveloper compiles and deploys the application to the DefaultServer instance: [tell me more...]
In the FODFacadeClient.java class,
comment out the for loop corresponding to
the queryOrderItemsFindAll
method, and comment out the for loop corresponding
to the queryOrdersFindAll method, to see
only the results of the queryOrderItemsFindByQuantity method.
Editing the Class
To better visualize the result of the FindByQuantity method,
you can edit the
FODFacadeClient.java class, and comment out: [tell me more...]
Click Make to recompile the class.
Right-click FODFacadeClient.java and
choose Run.
In the IDE
After editing the code and running the sample client again,
the Log window should display only the returned rows retrieved
by the '3' clause (that is, order items with quantity greater than 3): [tell me more...]
Run the Java Service Outside the Java EE Container
Running the Java Service Outside the Java EE Container
A persistence unit can be configured to run inside or
outside the container. In EJB 3.0, you can run entities
in a pure Java SE environment, without using an application
server. One reason you might do this is to create a simple
Java SE testbed (using JUnit, perhaps) to test your entity
behavior without the overhead of deploying/executing in
an application server. Another reason would be to run a
Swing application locally. [tell me more...]
In the Application Navigator, right-click persistence.xml and
choose New
Java Service Facade.
Accept the default service class name, and confirm that Generate
a main() method with sample client code is selected. Then click Next to create a new persistence unit.
On
step 2, enter outside as
the persistence unit name. 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 a Java Service Facade
From within the Create Java Service Facade wizard, you
can create a
new persistence unit for use either inside or outside a
Java EE container. [tell me more...]
On step 3, deselect the Orders.findAllByRange, OrderItems.findAllByRange
and OrderItems.findByQuantityByRange methods and
click Next.
Then click Finish.
Edit the JavaServiceFacade.java file
in the source editor to add a new line
to the class to retrieve order items by quantity and get the quantity. Use sample code
Click Make to
compile the JavaServiceFacade class.
In the Application Navigator, right-click JavaServiceFacade.java and choose Run.
Running a Java Service Facade
When the Java service
facade runs, the Log window displays the result of the
execution of the class running outside the Java EE container,
returning the quantity values that are greater than 3 in the retrieved records: [tell me more...]
In the Application Navigator, double-click persistence.xml to
view the content of the file.
In the IDE
When you double-click persistence.xml in
the Application Navigator, the file opens in the default editor. Click Overview to view the file in the overview editor
for the JPA Persistence Descriptor. [tell me more...]
Expose the EJB as a Data Control
Exposing the EJB as a Data Control
After creating the data model, you can expose the EJB
as a data control for the Oracle ADF framework. This
simplifies the binding of the user interface
to the EJB. [tell me more...]
In the Application Navigator, right-click FODFacadeBean.java
and choose Create Data Control.
In the Choose EJB Interface dialog, select Local,
and click OK.
Choosing
an EJB Interface
The local interface is used for clients that run in the
same virtual machine, such as web clients. You will
be building web pages for this application, so Local
is the appropriate selection. [tell me more...]
Click Save All to save your work.
Collapse the EJBModel project
node before continuing.
In the IDE
The root node of the Data Controls panel represents the
data control registered for the business service. While
the data control itself is not an item you can select,
you may select among the operations it supports. [tell me more...]