Introduction to EJB 3.0 Using JDeveloper and Oracle Application Server






Introduction to EJB 3.0 Using JDeveloper 10g and OC4J

Using JDeveloper, this tutorial provides an introduction to building the data model for an application using the new Enterprise JavaBeans 3.0 specification.

Approximately 30 minutes

Topics

The tutorial covers the following topics:

Creating the Connections

Configuring the Application and Project

Creating the Persistence Model
Creating the Data Model

Creating the Client

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 and deploying Session and Entity beans. These beans use the EJB 3.0 annotations and the new POJO (Plain Old Java Object) model persistence. You will use the Entity Manager API to create, update, delete and query the POJO persistence model.

As part of the tutorial, you will create a database connection and an OC4J application connection. Once complete, the application could be deployed to Oracle Application Server 10g for enterprise-wide access.

Back to Topic List

You 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 session beans.

Back to Topic List

Prerequisites

Before you begin, you should:

1.

Have access to or have installed Oracle JDeveloper 10g Release 3 (10.1.3) Production edition. You can download it from Oracle Technology Network.

 

2.

Have access to or have installed the Oracle Sample Schemas, included with Oracle Database 10g.

The tutorial uses the HR schema. Specifically, the pages work with the EMPLOYEES table.

Instructions for installing the HR schema and creating a connection to it in JDeveloper are available online at:

http://www.oracle.com/technology/obe/obe1013jdev/common/OBEConnection.htm

3.

Start JDeveloper. Double-click the JDeveloper executable (<JDEV_HOME>\jdeveloper.exe) found in the root directory where you unzipped it.

 

If the Migrate User Settings dialog box opens, click NO.

Close the Tip of the Day window.

 

4.

The JDeveloper IDE should now be displayed.

 

Creating the Connections

In this section, you create a database connection to the Human Resources (HR) schema.

Back to Topic List

1.

In the Application 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, verify the connection settings, click Back to make any necessary changes, and then retest the connection.

If the connection is successful, click Finish to complete the connection.

 

Back to Topic List

Configuring the Application and the Project

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 Application 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.

 

4.

In the Application Navigator, right-click the EJB_Project node and select the Project Properties option from the context menu.

 

5.

In the Project Properties dialog, select the Compiler node and check the Use Javac option, then OK.

 

6.

Click the Save All icon to save your work.

 

Back to Topic List

Creating the Persistence Model

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 Application 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 CMP Entity Beans from tables. Click OK.


3.

In Step 1 of 5 of the Create CMP Entity Beans from Tables wizard, select the Enterprise JavaBeans 3.0 (J2EE 5.0) option, and then click Next.

 

4.

In Step 2 of 5, select hrconn as the connection name.

 

5.

In step 3 of 5, 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 4 of 5, enter buslogic.persistence as the package name. Click Next.

 

7.

Click Next in step 5 of 5 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 Application Navigator to open it in the source editor.

 

9.

A NamedQuery metadata statement has been created by default. It retrieves all rows from the departments table.

@NamedQuery(name="findAllDepartments", query="select object (o)
from Departments o")

 

10.

Because you will insert new rows in the Departments table, you need to specify that the primary key values come from a database sequence.

Scroll down to the @Id statement and replace it by the following statements:

@Id (generate=SEQUENCE,generator="DEPARTMENT_SEQ_GEN")
@SequenceGenerator(name="DEPARTMENT_SEQ_GEN", sequenceName="DEPARTMENTS_SEQ",allocationSize=1)

Press Alt + Enter to automatically add the required import statement and manually add the following import statement:

import static javax.persistence.GeneratorType.*;

 

11.

Double-click the Employees.java node in the Application 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 below, so that your resulting code looks like this:

@NamedQueries({
@NamedQuery(name="findAllEmployees", query="select object(o) from Employees o")
,
@NamedQuery(name="getEmail",query="SELECT OBJECT(employees) FROM Employees "
+
" employees WHERE employees.employeeId =:empid")
})

Press Alt + Enter to accept the suggested import statement:
import javax.persistence.NamedQueries;

 

13.

Right-click the EJB_Project node in the Application Navigator and choose the Make option to compile your Java classes.

 

14.

Verify that the Message - Log window does not report any error.

 

Back to Topic List

Creating the Data Model

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 Application 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.

 

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, select only the Implement a Remote Interface option. Click Next and then Finish.

 

6.

The Application 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 code. HRAppFacade - a local interface containing the detail code

Open the Structure pane (View | Structure) and click the HRAppFacadeBean in the Application Navigator. The Structure pane should look like this displaying the HRAppFacade classes:

 

8.

In the Structure pane, double-click the HRAppFacade interface to edit the code and add the following method declarations:

public void addDepartment(String department_name, long location_id) throws NamingException;
public String getEmail(long empid) throws NamingException;

Press Alt + Enter to add the suggested import statement.

 

9.

Double-click the HRAppFacadeBean class to open it in the code editor. Then click the light bulb icon next to the class declaration and select Implement Methods.

Select both addDepartment and getEmail methods, and click OK.

 

10.

Change the code of the newly implemented methods to the following:

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);
}

public String getEmail(long empid)
{
Employees emp = (Employees)this.em.createNamedQuery("getEmail").setParameter("empid",empid).getSingleResult();
return emp.getEmail();
}

Press Alt + Enter to add the suggested import statement

 

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.

 

Back to Topic List

Creating the Client

1.

The first step in creating the client is 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.

In the Application Navigator, right-click HRAppFacadeBean and select Run.

This will launch the embedded server and deploy the HRAppFacadeBean.

 

2.

The next steps are to create a test client.

In the Application Navigator, right-click HRAppFacadeBean and select New Sample Java Client.

 

3.

In the Sample EJB Java Client Details dialog box, select Connect to OC4J Embedded in JDeveloper.

Click OK.


4.

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.

// System.out.println( hRAppFacade.findAllDepartments( ) );
// System.out.println( hRAppFacade.findAllEmployees( ) );

 

5.

In HRAppFacadeClient, add the following statements to create a new department by using the addDepartments() method.

String department_name = "IT Administration 12";
long location_id = 1500;
hRAppFacade.addDepartment( department_name, location_id );

 

6.

Specify the employee ID of the employee for whom you want to view the e-mail. For example:

long empid = 100;

Call the getEmail() method and print the e-mail for the employee_id argument:

System.out.println(hRAppFacade.getEmail( empid ));

 

7.

Some Java Virtual Machines may not recognize the -javaagent runtime option that JDeveloper uses to run this sample client.

To avoid any runtime issues with this option, follow the next few steps to change how JDeveloper uses the JVM.

Right-click the EJB Project in the Application Navigator and select Project Properties from the context menu to open the Project Properties for the EJB_Project.

 

8.

Select the Run/Debug category.

Select the default Run Configuration and click Edit.

 

9.

Select the Launch Settings category in the Edit Run Configuration wizard.

 

10.

Use the Virtual Machine drop list and select Client as the JVM type.

Click OK to apply the change and close the Edit Run Configuration window. Click OK again to close the Project Properties window.

 

11.

In the Application Navigator, right-click the HRAppFacadeClient file and select Run.

 

12.

Ensure that the e-mail (SKING) for the customer you specified is being returned.

 

13.

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.

 

Back to Topic List

In this tutorial, you created persistence services and accessed them using EJB 3.0. Additionally, you used a client to test the deployed EJBs.

You've learned how to:

Back to Topic List

Place the cursor over this icon to hide all screenshots.

 

 

 

E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy