Legal | Privacy
Create Business Services using Enterprise JavaBeans

Create Business Services using Enterprise JavaBeans

In this tutorial, using an EJB (Enterprise Java Bean) Session Bean, you create a business layer on top of TopLink persistence services.

This tutorial takes approximately half an hour to complete.

Topics

This tutorial discusses the following:

Configure the Project

Create the Facade

Create the Business Services

Place the cursor over this icon to load and view all the screenshots for this tutorial. (Caution: Because this action loads all screenshots simultaneously, response time may be slow depending on your Internet connection.)

Note: Alternatively, you can place the cursor over each individual icon in the following steps to load and view only the screenshot associated with that step.


Overview

In this tutorial you use the Session facade design pattern to wrap the persistence object model. You also write new business methods in the facade that access data using persistence services.

Back to Topic List

Based on the racing schema foundation, in this tutorial, you create a Session EJB to implement the Session facade design pattern, and you add a method to obtain a session. You write and test new business methods in the facade that use persistence services to access data, and you retrieve team orders and a drivers list. Finally, you create a method to add a new row to the Team Order table. You also add a method that returns season results as a Collection.

Back to Topic List

Prerequisites

Before starting this tutorial, you should:

1.

Have access to or have installed Oracle JDeveloper 10g.

2. Have access to an Oracle8i, Oracle9i, or Oracle 10g database, and know the passwords for the system and sys users.
3. Have SQL*Plus installed on your local machine.

This tutorial is not based on the Oracle Sample Schemas so make sure you complete the following steps:


Set the environment

Before you begin this tutorial, perform the following steps:

1.

Download the BusinessServices_setup zip file onto your local system.

 

2.

Unzip the downloaded BusinessServices_setup.zip file into the <JDEV_HOME>\mywork\ directory.

 

3.

Using a DBA account, use Oracle Enterprise Manager, which is part of Oracle 10g or Oracle9i, to create the racing user, alternatively uou can use SQL*Plus to issue the following statements:

CREATE USER racing IDENTIFIED BY racing;
GRANT CONNECT, RESOURCE TO racing;

Move your mouse over this icon to see the image


4.

Connect to SQL*Plus using racing as both the username and password.

Move your mouse over this icon to see the image

 

5.

Run the <JDEV_HOME>\mywork\BusinessServices_support\schema\racing\Schema.sql script in the SQL*Plus window to create racing objects and populate tables.

Move your mouse over this icon to see the image

 

Back to List

Create a Database Connection

This topic describes how to create a JDBC connection to the database schema that you use for this exercise. You need to create a connection with the following details:

Connection Name:
racing_conn
Username:
racing
Password:
racing
Deploy Password:
checked
Driver:
thin
Host Name:
localhost
JDBC Port:
1521
SID:
ORCL

To define a connection in JDeveloper, perform the following steps:

1.

Choose View | Connection Navigator.

Move your mouse over this icon to see the image


2.

Right-click the Database node and choose New Database Connection from the context menu.

Move your mouse over this icon to see the image

 

3.

In the Create Database Connection wizard, review the information on the Welcome page and click Next.

Move your mouse over this icon to see the image

 

4.

In the Connection Name field type the name for the connection as shown above. Click Next.

Move your mouse over this icon to see the image

 

5.

On the Authentication page:

In the Username and Password fields, type racing as shown above.

Select Deploy password.

Click Next.

Move your mouse over this icon to see the image

 

6.

On the Connection page:

In the Host name, JDBC Port, and SID fields, type the entries shown above. Click Next.

Move your mouse over this icon to see the image

 

7.

Click Test Connection.

If the database is available and the connection details are correct, you see Success! in the Status window.

Move your mouse over this icon to see the image

If an error occurs, verify the settings, click Back to make any changes necessary, and then retest the connection. If you still cannot connect, there may be a problem with the database, or it may not be running.

 

8.

Click Finish. The connection now appears below the Database connection node in the Connection Navigator. If desired, you can expand the connection and its nodes to look at the database objects in the schema.

Move your mouse over this icon to see the image

 

Back to List

 

This tutorial assumes that you have a working persistence services project, if you have not created one, you could build on top of a pre-created persistence project . To use a pre-created persistence project, perform the following steps.

Install the Pre-Created Business Services Lab

To install the pre-created Business Services Lab , perform the following steps:

1.

In the JDeveloper menu, select File | Open. Navigate to the directory <JDEV_HOME>\mywork\BusinessServices\TopLinkObjects and select TopLinkObjects.jws. Click Open.

Move your mouse over this icon to see the image

 

Back to List

Back to Topic List

Configure the Project

To create a new project and configure it to develop the business services, perform the following steps:

1.

Right-click the TopLinkObjects workspace in the Applications Navigator and choose New Project from the context menu.

Move your mouse over this icon to see the image

 

2.

In the New Gallery, select Empty Project in the Items list and click OK.

 

3.

In the Create Project dialog, rename the project as BusinessServices and click OK.

Move your mouse over this icon to see the image

 

4.

Select the BusinessServices project and choose Tools | Project Properties from the menu. In the Project Properties dialog, set the following:

Common > Input Paths > Default Package

racing.business
Common > Dependencies PersistenceServices.jpr

Common > Technology Scope Select Enterprise JavaBeans in the Available Technologies and click the shuttle buttonShuttle Button to move to Selected Technologies
Libraries Select TopLink in the Available Libraries and click the shuttle buttonShuttle Button to move to Selected Libraries

Click OK.

 

Back to Topic List

Create the Facade

This tutorial uses the Session facade design pattern. To create the facade, perform the following steps:

1.

In the Applications Navigator, right-click the BusinessServices project and choose New from the context menu.

In the New Gallery, expand the Business Tier node in the Categories list and select Enterprise JavaBeans. In the Items list, select EJB Diagram and click OK.

Move your mouse over this icon to see the image

 

2.

In the Create EJB Diagram dialog, set the following:

  Name
RACING EJB Diagram
  Package
racing.business.uml

 

Move your mouse over this icon to see the image

 

3.

In the Component Palette, select Session Bean and click on the EJB Diagram surface. (If the Component Palette is not visible, launch it by choosing View | Component Palette from the menu.)

 

4.

Skip the Welcome page, and in Step 1 of 4 of the Create Enterprise JavaBean Wizard, select Enterprise JavaBeans 2.0 from the drop down list, then click Next.

5.

In Step 2 of 4 of the wizard, rename the EJB as racingFacade, then click Next.

Move your mouse over this icon to see the image

 

6. In Step 3 of 4 of the wizard, accept the defaults and click Next.
7.

In Step 4 of 4 of the wizard, select both check boxes to include both types of interfaces (local and remote), click Next, and then click Finish on the summary page.

Move your mouse over this icon to see the image

 

Back to Topic List

Create the Business Services

Now you begin to add business methods that use persistence services. You also do some unit testing. To create and test business services, you perform the following steps:


Add a Method to Get a Server Session

1.

In the EJB Diagram, double-click racingFacade EJB to launch the EJB Module editor.

 

2. Expand racingFacade in the left hand navigator and select Methods. On the right hand side panel , choose Business Methods in the Method category and click Add.

 

3.

In the Business Method Details dialog set the following:

  Name
getServerSession
  Return Type
oracle.toplink.threetier.Server

Deselect the check boxes for Expose through Remote Interface and Expose through Local Interface. Click OK, then click OK in the EJB Module Editor.

Move your mouse over this icon to see the image

 

4.

Right-click racingFacade EJB in the Applications Navigator and choose Go To Bean Class from the context menu.

Move your mouse over this icon to see the image

 

5.

Modify the getServerSession() method as follows:

public Server getServerSession()
{
String tl_ddFileName =
    
"/META-INF/PersistenceServices/toplink-deployment-descriptor.xml";
ClassLoader appClassLoader = Thread.currentThread().getContextClassLoader();
oracle.toplink.sessions.Project project = XMLProjectReader.read(tl_ddFileName,
    appClassLoader);
DatabaseLogin loginInfo = project.getLogin();
loginInfo.getPlatform().getConversionManager().setLoader(appClassLoader);
oracle.toplink.threetier.Server server = project.createServerSession();
server.login();
return server;
}

Add the following import statements:


import oracle.toplink.sessions.DatabaseLogin;
import oracle.toplink.tools.workbench.XMLProjectReader;

 

6. Click the Save All button Save All button to save your work.

Back to List

Add a Method to Retrieve Team Orders

You can add an attribute which is of Collection type to the EJB, so that the collection can be used to return all Team Orders. To add this attribute and the method to retrieve the orders, perform the following steps:

1. Select racingFacade in the EJB Diagram and click on the second box from the top to invoke edit mode. Enter teamOrders : Collection and press Enter.

Move your mouse over this icon to see the image

 

2.

Right-click racingFacade EJB in the Applications Navigator and choose Go To Bean Class from the context menu.

Move your mouse over this icon to see the image

 

3. Modify the getTeamOrders() method as follows:

public Collection getTeamOrders()
{
oracle.toplink.threetier.Server server = getServerSession();
Session tlsession = (Session) server.acquireClientSession();
tlsession.logMessages();
UnitOfWork uow = tlsession.acquireUnitOfWork(); // to write to database
Vector orders = (Vector)tlsession.readAllObjects(TeamOrder.class);
return orders;
}

Enter the following imports statements or use ALT + Enter Key:

import racing.objectmodel.TeamOrder;
import oracle.toplink.sessions.Session;
import oracle.toplink.sessions.UnitOfWork;
import java.util.Vector;

 

4. Click the Save All button Save All button to save your work.

Back to List

Perform Unit Testing

Now you can perform unit testing of the business services created so far. To perform the testing, you create a command line client that uses remote interfaces to invoke the racingFacade bean. Because the TeamOrders collection returns a collection of racing.objectmodel.TeamOrder classes, the classes must implement java.io.Serializable.

In order to test the services implemented using J2EE components, JDeveloper comes with an embedded OC4J Server that enables you to run, debug, and profile J2EE components such as EJB. This feature provides the fastest application development life cycle, so that developers can add business services incrementally to the project and do unit testing without having to deploy the application.

1.

In the PersistenceServices project, select TeamOrder.java in the racing.objectmodel package and choose Tools | Implement Interface.... from the menu.

Move your mouse over this icon to see the image

 

2.

From the Available interface list select the java.io.Serializable class and click OK.

 

3. Repeat the above steps for the Results.java and Drivers.java classes.
4.

Click the Save All button Save All button to save your work.

 

5.

To test the business services you have implemented so far, run the racingFacade bean: Right-click the racingFacade EJB node in the Applications Navigator and choose Run from the context menu.

Move your mouse over this icon to see the image

In the log window you see the Embedded OC4J Server startup messages such as:

04/10/15 18:31:49 Auto-deploying - compiling and loading...
04/10/15 18:31:49 done.
Ready message received from Oc4jNotifier.Embedded OC4J startup time: 7551 ms.
04/10/15 18:31:49 Oracle Application Server Containers for J2EE 10g (10.1.2.0.0) initialized

Note: If the log window is not displayed, choose View | Log from the menu.

Move your mouse over this icon to see the image

 

6.

Right-click the racingFacade EJB node in the Applications Navigator and choose New Sample Java Client from the context menu.

Move your mouse over this icon to see the image

 

7.

In the Sample EJB Java Client Details dialog, select the Connect to OC4J embedded in JDeveloper option and click OK. This generates a Java class racingFacadeClient.java.

Move your mouse over this icon to see the image

 

8.

After the line:
//RacingFacade.getTeamOrders( );
Add the following code to the generated client:

Collection col = RacingFacade.getTeamOrders( );
Iterator it = col.iterator();
while (it.hasNext())
{
TeamOrder team = (TeamOrder)it.next();
System.out.println(team.getConstructorname());

}

Enter the following imports statements or use ALT + Enter Key:

import java.util.Collection;
import java.util.Iterator;
import racing.objectmodel.TeamOrder;

 

9.

Right-click racingFacadeClient.java in the Applications Navigator and choose Run from the context menu. You can see the output in the log window (if the log window is not displayed, choose View | Log from the menu).

Move your mouse over this icon to see the image

 

Back to List

Add Presentation Clients Methods

So far you have used the Session facade design pattern to wrap some persistence services. You have also performed unit testing of business services. Now you add business methods that you will use in the next lab to build presentation clients.

1.

Add an attribute which is of type String. You use this attribute as a transient attribute to store the name of the new team. To add the attribute, select racingFacade in the EJB Diagram and click in the second box from the top to invoke edit mode. Enter teamName : String and press Enter.

Move your mouse over this icon to see the image

 

2.

Add a method constructTeam that creates a new team in the Team_Order table. This method takes a Driver object as a parameter.

Select racingFacade in the EJB Diagram and click in the third box from the top to invoke edit mode. Enter constructTeam(racing.objectmodel.Drivers driver) : void and press Enter.

Move your mouse over this icon to see the image

 

3.

Add an attribute of type Collection that contains racing.objectmodel.Drivers classes. Select racingFacade in the EJB Diagram and click in the second box from the top to invoke edit mode. Enter driversList : Collection and press Enter.

Move your mouse over this icon to see the image

 

4.

Right-click the racingFacade EJB in the Applications Navigator and choose Go To Bean Class from the context menu.

Move your mouse over this icon to see the image

 

5.

Modify the getDriversList() method as follows

public Collection getDriversList()
{

oracle.toplink.threetier.Server server = getServerSession();
Session tlsession = (Session) server.acquireClientSession();
tlsession.logMessages();
UnitOfWork uow = tlsession.acquireUnitOfWork();
Vector drivers = (Vector)tlsession.readAllObjects(Drivers.class);
tlsession.release();
return drivers;
}

 

6.

Modify the constructTeam() method as follows

public void constructTeam(Drivers driver)
{

oracle.toplink.threetier.Server server = getServerSession();
Session tlsession = (Session) server.acquireClientSession();
tlsession.logMessages();
UnitOfWork uow = tlsession.acquireUnitOfWork();
TeamOrder to = new TeamOrder();
TeamOrder toClone = (TeamOrder) uow.registerObject(to);
Drivers driverClone = (Drivers) uow.registerExistingObject(driver);
toClone.setConstructorname(this.getTeamName());
toClone.setDriver1(driverClone);
uow.commit();
tlsession.release();
}

 

7.

Add a default value to the teamName attribute in the bean class:


public String teamName = "Enter New Team Name" ;

Move your mouse over this icon to see the image

 

8. Click the Save All button Save All button to save your work.

Back to List

Test the Added Methods

To unit test the newly added business methods, perform the following steps:

 

1.

Right-click the racingFacade EJB node in the Applications Navigator and choose Run from the context menu.

Move your mouse over this icon to see the image

In the log window you can see the Embedded OC4J Server startup messages such as:

04/10/15 19:06:03 Auto-deploying - file:/D:/oracle/JDev10g/jdev/mywork/BusinessServices/TopLinkObjects/BusinessServices
/public_html/ (No previous deployment found)...
04/10/15 19:06:03 Auto-deploying - compiling and loading...
04/10/15 19:06:03 done.Embedded OC4J startup time: 2403 ms.

Note: If the log window is not displayed, choose View | Log from the menu.


2. Right-click the racingFacade EJB node in the Application Navigator and choose New Sample Java Client from the context menu.

Move your mouse over this icon to see the image

 

3. In the Sample EJB Java Client Details dialog, select Connect to OC4J embedded in JDeveloper and click OK. This generates the Java class racingFacadeClient1.java.
4.

After the line:
// RacingFacade.constructTeam( racing.objectmodel.Drivers driver );
Add the following code to the generated client:

RacingFacade.setTeamName( "TestTeam");
Collection col = RacingFacade.getDriversList( );
Iterator it = col.iterator();
Drivers driver = (Drivers)it.next();
RacingFacade.constructTeam(driver );

Add the following import statements:

import java.util.Collection;
import java.util.Iterator;
import racing.objectmodel.Drivers;

 

5.

Right-click racingFacadeClient1.java in the Applications Navigator and choose Run from the context menu.

Move your mouse over this icon to see the image

 

6.

Using the racing_conn database connection in the Connections Navigator, check the data in the Team_Order table and see if the new row has been inserted.

Move your mouse over this icon to see the image

 

Back to List

Add a Method to Return Season Results

Now you add one more service method to return a Collection of Results for a grandPrixSeason. In this method you see some additional capabilities of TopLink Persistence Architecture. In the method you use TopLink Expression builder to construct dynamic queries using Java. To add this method, perform the following steps:

1. In the EJB Diagram, double-click the racingFacade EJB to launch the EJB Module editor.
  • Select racingFacade > Methods in the left hand navigator
  • On the right hand side panel , choose Business Methods in Method category and click Add.


2.

In the Business Method Details dialog, set the following:

  Name
grandPrixResultsService
  Return Type
java.util.Collection
  Parameters
Integer season 

Select the checkboxes for Expose through Remote Interface and Expose through Local Interface. Click OK, then click OK in the EJB Module editor.

Move your mouse over this icon to see the image

 

3.

Right-click the racingFacade EJB in the Applications Navigator and choose Go To Bean Class from the context menu.

Move your mouse over this icon to see the image

 

4.

Modify the grandPrixResultsService() method as follows:

public Collection grandPrixResultsService(Integer season)
{
oracle.toplink.threetier.Server server = getServerSession();
Session tlsession = (Session) server.acquireClientSession();
tlsession.logMessages();
UnitOfWork uow = tlsession.acquireUnitOfWork();
Expression exp = new ExpressionBuilder().get("season").equal(season);
Vector results = tlsession.readAllObjects(Results.class, exp);
return results;
}

Add the following import statements:

import oracle.toplink.expressions.Expression;
import oracle.toplink.expressions.ExpressionBuilder;
import racing.objectmodel.Results;

 

5. Click the Save All button Save All button to save your work.

Back to List

 

Test the SeasonResults Method

To unit test the newly added business method, perform the following steps:

1. Right-click the racingFacade EJB node in the Applications Navigator and choose Run from the context menu.

Move your mouse over this icon to see the image

In the log window you can see the Embedded OC4J Server startup messages such as:

04/10/15 19:06:03 Auto-deploying - file:/D:/oracle/JDev10g/jdev/mywork/BusinessServices/TopLinkObjects/BusinessServices/ (No previous deployment found)...
04/10/15 19:06:03 Auto-deploying - compiling and loading...
04/10/15 19:06:03 done.Embedded OC4J startup time: 2403 ms.

Note: If the log window is not displayed, choose View | Log from the menu.


2. Right-click the racingFacade EJB node in the Application Navigator and choose New Sample Java Client from the context menu.

Move your mouse over this icon to see the image

 

3. In the Sample EJB Java Client Details dialog, select Connect to OC4J embedded in JDeveloper and click OK. This generates the Java class racingFacadeClient2.java.
4.

After the line:
// RacingFacade.constructTeam( racing.objectmodel.Drivers driver );
Add the following code to the generated client:

Integer season = new Integer(2004);
Collection col = RacingFacade.grandPrixResultsService(season);
Iterator it = col.iterator();
while (it.hasNext())
{
Results results = (Results)it.next();
System.out.println("Laps: " + results.getLaps());
System.out.println("Car: " +results.getCarNumber());
System.out.println("Season: " +results.getSeason());
}

Add the following import statements:


import java.util.Collection;
import java.util.Iterator;
import racing.objectmodel.Results;

 

5.

Right-click racingFacadeClient2.java in the Applications Navigator and choose Run from the context menu.

Move your mouse over this icon to see the image

 

6.

Open the Log Window to check that the grandPrixResultsService is displayed.

Move your mouse over this icon to see the image

 

Back to List

Back to Topic List

Summary

In this tutorial you created a Session EJB to implement the Session facade design pattern, and you added a method to obtain a session. You wrote and tested new business methods in the facade that use persistence services (created in the previous lab) to access data, retrieving team orders and a drivers list. You created a method to add a new row to the Team Order table. You also added a method that returns season results as a Collection.

Back to Topic List

Related Information

Back to Topic List

Move your mouse 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