|
|
Create a Database ConnectionThis 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:
To define a connection in JDeveloper, perform the following steps:
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 LabTo install the pre-created Business Services Lab , perform the following steps:
To create a new project and configure it to develop the business services, perform the following steps:
This tutorial uses the Session facade design pattern. To create the facade, perform the following steps:
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:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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:
Deselect the check boxes for Expose through Remote Interface and Expose through Local Interface. Click OK, then click OK in the EJB Module Editor.
|
||||||
| 4. |
Right-click racingFacade EJB in the Applications Navigator and choose Go To Bean Class from the context menu.
|
||||||
| 5. | Modify the getServerSession() method as follows: Add the following import statements:
|
||||||
| 6. | Click the Save All button |
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.
|
| 2. |
Right-click racingFacade EJB in the Applications Navigator and choose Go To Bean Class from the context menu.
|
| 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;
|
| 4. | Click the Save All button |
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
|
| 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
|
| 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. In the log window you see the Embedded OC4J Server startup messages such
as: Note: If the log window is not displayed, choose View | Log from the menu.
|
| 6. | Right-click the racingFacade EJB node in the Applications Navigator and choose New Sample Java Client from the context menu.
|
| 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.
|
| 8. | After the line: Collection col = RacingFacade.getTeamOrders( ); Enter the following imports statements or use ALT + Enter Key:
|
| 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).
|
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.
|
| 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.
|
| 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.
|
| 4. | Right-click the racingFacade EJB in the Applications Navigator and choose Go To Bean Class from the context menu.
|
| 5. | Modify the getDriversList()
method as follows
|
| 6. | Modify the constructTeam()
method as follows
|
| 7. | Add a default value to the teamName attribute in the bean class:
|
| 8. | Click the Save All button |
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. In the log window you can see the Embedded OC4J Server startup messages
such as: 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.
|
| 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.setTeamName( "TestTeam"); Add the following import statements: import java.util.Collection;
|
| 5. | Right-click racingFacadeClient1.java in the Applications Navigator and choose Run from the context menu.
|
| 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.
|
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.
|
|||||||||
| 2. | In the Business Method Details dialog, set the following:
Select the checkboxes for Expose through Remote Interface and Expose through Local Interface. Click OK, then click OK in the EJB Module editor.
|
|||||||||
| 3. | Right-click the racingFacade EJB in the Applications Navigator and choose Go To Bean Class from the context menu.
|
|||||||||
| 4. | Modify the grandPrixResultsService() method as follows: Add the following import statements:
|
|||||||||
| 5. | Click the Save All button |
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.
In the log window you can see the Embedded OC4J Server startup messages
such as: 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.
|
| 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: Integer season = new Integer(2004); Add the following import statements:
|
| 5. | Right-click racingFacadeClient2.java in the Applications Navigator and choose Run from the context menu.
|
| 6. | Open the Log Window to check that the grandPrixResultsService is displayed.
|
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.
| To learn more about JDeveloper, refer to additional OBEs on the OTN Web site. | ||
|
|
|
|
|