Introduction to EJB 3.0 Using JDeveloper and Oracle Application Server
Introduction to EJB 3.0 Using JDeveloper 10g and
OC4J
Purpose
Using JDeveloper, this tutorial provides an introduction to
building the data model for an application using the new Enterprise JavaBeans
3.0 specification.
Place
the cursor over this icon to load and view
all the screenshots for this tutorial. (Caution: This action loads all
screenshots simultaneously, so, depending on your Internet connection,
may result in a slow response time.)
Note: Alternatively, you can place the cursor
over an individual icon in the following steps to load and view only the
screenshot associated with that step. You can hide an individual screenshot
by clicking it.
Overview
In the tutorial, you implement a persistence model by developing
Session and Entity beans. These beans use the EJB 3.0 annotations and POJO (Plain
Old Java Object) model persistence. An Enterprise JavaBean (EJB) is a server-side
component that encapsulates business logic. You use the Entity Manager API to
create, update, delete and query the POJO persistence model.
As part of the tutorial, you create a database connection
and an OC4J application connection where you run a sample Java client. Once
complete, the application could be deployed to any Java EE container (for example,
Oracle Application Server 10g) for enterprise-wide access.
You need to create persistence objects for the DEPARTMENTS
and EMPLOYEES tables. The persistence objects are implemented as Entity Beans.
Methods are created for adding a new department, retrieving department data,
and returning the email for an employee. These methods are implemented as part
of a session bean.
In the Applications Navigator, click the Connections
tab to create a database connection.
2.
Right-click the Database node in the Connection Navigator,
and select the New Database Connection option.
3.
Click Next on the Welcome page of the Create Database
Connection wizard. In Step 1 of 4, enter hrconn as the
connection name, and then click Next.
4.
In Step 2 of 4 of the Create Database Connection wizard, enter hr
as the username and hr as the password. Select the Deploy
Password check box, and then click Next.
5.
In Step 3 of 4 of the Create Database Connection wizard, make sure that
the following values are specified:
Driver
thin
Host Name
localhost
JDBC Port
1521
SID
ORCL
Click Next.
6.
In Step 4 of 4 of the Create Database Connection wizard, click the Test
Connection button.
If the database is available and the connection
details are correct, you see the word Success! displayed in the
Status window.
If an error occurs, click Back to verify the connection
settings and make any necessary changes, and then retest the connection.
If the connection is successful, click Finish to complete the connection.
When you work in JDeveloper, you organize your work
in projects within applications. JDeveloper provides several templates that
you can use to create an application and projects. The templates are preconfigured
with a basic range of technologies that are needed for developing various types
of applications, and you create your working environment by selecting the template
that fits your needs. You can then configure it to add any other technologies
that you plan to use.
In this topic, you choose the basic application and
a new project, with no predefined technology.
To create a new application with a new project, perform
the following steps:
1.
In JDeveloper, click the Applications
tab. In the Applications navigator, right-click Applications
and select New Application from the shortcut menu.
2.
In the Create Application
dialog box, enter HRApp as the application name. Specify
buslogic as the application package prefix, and select
the No Template option. Click OK.
3.
In the Create Project dialog box, rename the project
from Project1 to EJB_Project, hence providing a more
meaningful name. Then click OK.
The business or persistence model provides data access and validation
for an application. When data is managed by the business model, the data
is always
validated by the model, regardless of the client implementation. This cleanly
separates the validation and business rules from the user interface.
In this section of the tutorial, you create the persistence model
for departments and employees using EJB 3.0 entity beans. To create EJB 3.0 entity beans, perform the following steps:
1.
In the Applications Navigator, right-click the
EJB_Project node and choose the New option.
2.
In the New Gallery dialog box, expand the Business Tier
node in Categories. Click EJB in the Items list, then
select Entities from Tables (JPA/EJB 3.0). Click OK.
3.
On the Welcome page of the wizard click Next to skip
the page.
4.
In Step 1 of 4 of the Create
Entities from Tables wizard, select hrconn as
the connection name.
5.
In step 2 of 4, for the HR Schema and the Tables
Objects Types, click the Query button, and
then select the Departments and Employees
tables from the Available list and shuttle them to the Selected list.
Click Next.
6.
In step 3 of 4, enter buslogic.persistence as the package
name. Click Next.
7.
Click Next in step 4 of 4 and then Finish
to create the entity beans.
Click the Save All icon to
save your work.
8.
Double-click the Departments.java node in the Applications
Navigator to open it in the source editor.
9.
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. It retrieves all rows from the departments
table.
@NamedQuery(name = "Departments.findAll",
query = "select o from Departments o")
Note: Any symbol in Java code beginning with @
is known as an annotation. The use of annotations is a new language
feature introduced in Java SE 5.0 that allows you to add metadata to
your objects. EJB 3.0 uses the annotation syntax. Examples of annotations
follow:
Annotation
Description
@Entity
Identifies the file as an EJB 3.0 entity
@NamedQuery
A query that can be used at run time to retrieve
data
@Table
Specifies the primary table for the entity
@Id
Can define which property is the identifier for
the entity
@Column
Specifies a mapped column for a persistent property
or field
@ManyToOne
Specifies a type of foreign key relationship between
tables
@JoinColumn
Specifies the join column and referenced column
for a foreign key relationship
10.
For inserting new rows in the Departments table, you will use a database
sequence to provide primary key values. You could altenatively use a table
to provide your primary key values.
Scroll down to the @Id
statement and replace it with the following statements:
Double-click the Employees.java node in the Applications
Navigator to open it in the source editor.
12.
You will add another named query. To support this, you will add the EJB
3.0 metadata statements shown in bold text, so that your resulting code
looks like this:
@NamedQueries({
@NamedQuery(name = "Employees.findAll", query = "select
o from Employees o")
, @NamedQuery(name="Employees.findById",query="select
o FROM Employees o WHERE o.employeeId = :empid")
})
Press Alt + Enter to accept the suggested
import statement:
import javax.persistence.NamedQueries;
13.
Right-click the EJB_Project node in the Applications
Navigator and choose the Make option to compile your
Java classes.
14.
Verify that the Message - Log window does not report any error.
A session facade presents client objects with a unified interface
to the underlying EJBs (Enterprise Java Beans). The client interacts only with
the facade, which resides on the server and invokes the appropriate EJB methods.
In this section, you create a session bean that implements a method to find
employee and department records.
1.
Right-click the EJB_Project project node in the Applications
Navigator, and select the New option from the context
menu. Open the Business Tier category and choose the
Session Bean item. Click OK.
2.
Click Next on the Welcome page of the Create Enterprise
JavaBean wizard. In step 1 of 4, enter HRAppFacade as
the EJB name. Leave the options unchanged, and then click Next.
3.
In Step 2 of 4, make sure all entity methods are selected, then click
Next.
Any entities in this project appear as a node in
the tree control. You can select the checkbox to include all entity methods
this entity exposes, or expand the nodes and select a subset of methods.
Notice that the named query appear as exposable methods.
4.
In Step 3 of 4, make sure that the full name for Bean Class is buslogic.HRAppFacadeBean,
and then click Next.
5.
In step 4 of 4, deselect the Implement a Local Interface option so
that only the Implement a Remote Interface option is
checked. Click Next and then Finish.
The remote interface is used for client applications
that run in a separate virtual machine, such as Java clients whereas local
interface is used for client applications that run in the same virtual
machine, such as Web clients .
6.
The Applications Navigator should look like this:
If not, select the EJB_Project node and select the Refresh
option from the View menu option (View | Refresh).
7.
The session bean is made up of two files: HRAppFacadeBean - contains
the session bean code. HRAppFacade - describes the capabilities of the
bean for remote clients.
Open the Structure pane (View | Structure) and in the
Applications Navigator select the buslogic.HRAppFacadeBean.
In the Structure pane expand the Sources node. The Structure pane should
look like this displaying the HRAppFacade classes:
8.
In the Structure pane, double-click the HRAppFacadeBean
to edit the code and add the following method declarations:
public void addDepartment(String
department_name, long location_id) throws NamingException
{
Departments dept = new Departments();
dept.setDepartmentName(department_name);
dept.setLocationId(location_id);
this.persistEntity(dept);
}
Press Alt + Enter to add the suggested
import statement.
9.
You need now to expose the method declarations through the remote
interface for the newly created methods. In the Structure pane, click
the HRAppFacadeBean interface and expand the Methods
node. Right click the addDepartment method and select
Properties from context.
In the Bean Method Details dialog, check the Expose through
Remote interface check box to populate the method declaration
to the remote interface.
Click OK.
10.
Repeat the same operation for getEmail(). In
the Structure pane, right click the getEmail method and
select Properties from context.
In the Bean Method Details dialog, check the Expose through Remote
interface check box to populate the method declaration to the
remote interface.
Click OK.
11.
Right-click the EJB_Project project and select the
Make option to compile your project.
12.
Verify that the Messages - Log window does not report any error.
You create the test client and before running it, you have to
run the HRAppFacadeBean using the OC4J server that is provided within JDeveloper.
This server allows you to create J2EE applications and test them within JDeveloper,
thereby eliminating the need to run an external server during the development
/ testing cycle.
1.
The next steps are to create a test client.
In the Applications Navigator, right-click HRAppFacadeBean
and select New Sample Java Client.
2.
In the Create Sample Java Client dialog box, click the New
Project button to create the client class in a separate project.
In the Create Project dialog type Client for the project
name, click OK.
Back in the Create Sample Java Client dialog, set the Client Class Name
field to client.HRAppFacadeClient and select Connect
to OC4J Embedded in JDeveloper. Click OK.
3.
Double-click the HRAppFacadeClient node to open the
file in the source editor. In the Main method, comment out the call to
the findAllDepartments
and findAllEmployees methods.
In the Applications Navigator, right-click HRApp and
select Make.
7.
In the Applications Navigator, right-click HRAppFacadeBean
and select Run.
This will launch the embedded server and deploy the
HRAppFacadeBean.
8.
In the Applications Navigator, right-click the HRAppFacadeClient
file and select Run.
9.
Ensure that the e-mail (SKING)
for the customer you specified (100) is being returned.
10.
Ensure that a new department is added to the table, utilizing the
sequence that you specified for the ID. On the Connection page, expand
Database > HR > Tables > DEPARTMENTS, and click
the Data tab in the editor.