0% found this document useful (0 votes)
121 views20 pages

Lab: Using Jdeveloper 10G For Creating The Persistence Layer With Ejb 3.0

This document provides instructions for using JDeveloper 10g to create the persistence layer for an application using EJB 3.0 annotations and the Entity Manager API. It describes how to generate Entity classes from database tables, create a session bean to manage database operations, and write a client to test adding a record. The steps include connecting to an Oracle database, generating Entity classes, creating and testing a session bean, and adding a record via the client which commits it to the database.

Uploaded by

Tokala Rajesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
121 views20 pages

Lab: Using Jdeveloper 10G For Creating The Persistence Layer With Ejb 3.0

This document provides instructions for using JDeveloper 10g to create the persistence layer for an application using EJB 3.0 annotations and the Entity Manager API. It describes how to generate Entity classes from database tables, create a session bean to manage database operations, and write a client to test adding a record. The steps include connecting to an Oracle database, generating Entity classes, creating and testing a session bean, and adding a record via the client which commits it to the database.

Uploaded by

Tokala Rajesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 20

Lab: Using JDeveloper 10g for Creating the Persistence

Layer with EJB 3.0

Introduction

In this lab you learn how to use some of the new features in EJB 3.0
specification. You will also develop and deploy Session and Entity beans
using EJB 3.0 annotations, new POJO-based model persistence, and the
Entity Manager API to create, update, delete and query the POJO
persistence model.
In this example you will create persistence objects for the Loans and
Loan_Customer tables, implemented as Entities. Methods for adding a
new loan provider, retrieving loan data, and returning the credit rating for a
loan customer are implemented in a session bean, using the Entity
Manager API.

Getting Started

1. Start JDeveloper 10g by clicking the Developer icon on the desktop


or by executing the jdeveloper.exe file located in the <JDEV_HOME>
directory.
If you see a "Migrate User Settings" dialog, click No.
2. Close the Tip of the Day window.
3. The JDeveloper IDE should now be displayed.

Created by PTS APAC 1


4. In the Applications navigator, click the Connections tab to create a
database connection.

5. Right-click the Database node in the connection navigator and select


the New Database Connection option.

6. Click Next on the Welcome screen of the 'Create Database


Connection' wizard.On Step1 of 4 enter XE as the connection name
and Connection Type as Oracle (JDBC), then Next.

Created by PTS APAC 2


7. In Step 2 of 4 of the 'Create Database Connection' wizard, enter odd
as username, and welcome1 as password. Click the Deploy
Password checkbox, then Next.

8. In Step3 of 4 of the 'Create Database Connection' wizard, make sure


the following values are specified:
Host Name: localhost
JDBC Port: 1521
SID: XE

Created by PTS APAC 3


9. Click Next and test the connection. If the test fails check the
connection properties.
10. Click Finish.

Configure the Workspace and the Project

1. To create a new workspace with a new project, perform the following


steps:
2. In JDeveloper, click the Applications Navigator tab. Right-click
Applications and select New Application from the shortcut menu.

3. In the Create Application dialog, enter LoanApp as the application


name, specify buslogic as the application package prefix and choose
the No Template option. Click OK.

Created by PTS APAC 4


4. In the Create Project dialog box, rename the project from Project1 to
SOAServices and click OK.

Create the Persistence Model

1. In the Applications navigator, right click the SOAServices node and


choose the New option.
2. In the New Gallery dialog, expand the Business Tier node in the
Categories. In the Items list select Entity Beans from Tables
(JPA/EJB 3.0). Click OK.

Created by PTS APAC 5


3. In Step 1 of 4, select XE as the connection name.
Click Next.
4. In step 2 of 4, click Query. Select the Loans and LoanCustomer
tables from the Available list and shuttle them to the Selected list.

Click Next.

Created by PTS APAC 6


5. In step 3 of 4 enter buslogic.persistence as the package name and
accept all other defaults.

Click Next.
6. Accept the defaults in step 4 of 4 and click Finish.
7. Double click the Loans.java node in the application navigator to open
it in the source editor.

8. Note the following EJB 3.0 named query is generated for you
automatically:
@NamedQuery(name = "Loans.findAll", query = "select o from
Loans o")

9. Right-click the SOAServices project node in the applications


navigator and choose the Make option to compile your java classes.

Created by PTS APAC 7


Create the Business Model

1. Right-click the SOAServices project node in the application navigator


and select the New option from the context menu. Open the Business
Tier | EJB category and choose the Session Bean item. Then OK.

2. Click Next on the Welcome screen of the 'Create Enterprise


JavaBean' wizard. Enter LoanAppFacade as the EJB name. Notice
the option Generate Session Faade Methods is ticked. Leave the
options unchanged and click Next.

Created by PTS APAC 8


3. In step 2 of 4, ensure all methods are checked for inclusion in the
session bean and click Next.

4. In Step 3 of 4, make sure the full name for the Bean Class is
buslogic.LoanAppFacadeBean, then Next.

Created by PTS APAC 9


5. In step 4 of 4, ensure the Implement a Remote Interface option and
uncheck the Implement a Local Interface option, then click Next and
Finish.

6. The application navigator should look like this:

Created by PTS APAC 10


7. Right-click the SOAServices project and choose the Make option to
compile your project. Verify that there are no errors reported in the
message log window.

Create the Client

1. Right click LoanAppFacadeBean and choose Run to run the bean.

2. You should see "Oracle Containers for J2EE 10g (10.1.3.0.0)


initialized" in the Running log.

Created by PTS APAC 11


3. Right-click LoanAppFacadeBean and select New Sample Java Client.

4. In the Sample EJB Java Client Details dialog, select Connect to


OC4J Embedded in JDeveloper and click OK.

Created by PTS APAC 12


5. Double click the LoanAppFacadeClient node to open the file in the
source editor. In the Main method, comment out the calls to the
findAllLoans() and findAllLoanCustomer() methods. These
methods will be called in a later lab.
//System.out.println( loanAppFacade.findAllLoans( ) );
//System.out.println( loanAppFacade.findAllLoanCustomer( ) );

6. In LoanAppFacadeClient, add a new static method to add a new


loan as follows:
private static Loans addLoan(String provider, long term,
String loanType, double
interestRate) {
Loans loan = new Loans();
loan.setProvider(provider);
loan.setTerm(term);
loan.setLoanType(loanType);
loan.setInterestRate(interestRate);
return loan;
}

7. Click Alt + Enter to import buslogic.persistence.Loans

Created by PTS APAC 13


8. Add the following code to the main method. This code calls the
addLoan method, passing in values for the arguments, uses the
persistEntity method to commit the new values, and prints messages
to the log.
Loans loan = addLoan("Galactic Loans", 30, "fixed", 6.25);
System.out.println("Loan entity created");
loanAppFacade.persistEntity(loan);
System.out.println("Loan entity committed");

9. Right-click the LoanAppFacadeClient file and choose Run.

Created by PTS APAC 14


The end of the message log should display the following:
Loan entity created
Loan entity committed
Process exited with exit code 0.

10. Ensure that a new Loan is added to the table, utilizing the sequence
you specified for the Id. In the connection tab expand Database |
ODD | Tables | LOANS and click the Data tab in the editor. Note that
you can view the SQL in the OC4J command window.

Add a Named Query

1. Now add a new named query to the LoanCustomer entity, in addition


to the one that is provided by default. Double click the LoanCustomer
.java node in the application navigator to open it in the Source editor.

Created by PTS APAC 15


2. Just above the provided @NamedQuery annotation, add the
annotation @NamedQueries. The editor will provide you with two
parenthesis. Move the end parenthesis of the @NamedQueries
annotation to just after the provided @NamedQuery annotation. Next,
add an open curly brace just after the first parenthesis, and add an
end curly brace just before the last parenthesis. The code should now
look like this:
@NamedQueries({
@NamedQuery(name = "LoanCustomer.findAll",
query = "select o from LoanCustomer o")
})

3. Add a comma after the provided query, and then add the following
query between the comma and the end parenthesis:
@NamedQuery(name = "LoanCustomer.findBySSN",
query="select loancustomer from LoanCustomer loancustomer
where loancustomer.ssn = :ssn")

4. The code should look similar to the following:


...
@Entity
@NamedQueries({
@NamedQuery(name = "LoanCustomer.findAll",
query = "select o from LoanCustomer o"),
@NamedQuery(name = "LoanCustomer.findBySSN",
query="select loancustomer from LoanCustomer loancustomer
where loancustomer.ssn = :ssn")
})
@Table(name = "LOAN_CUSTOMER")
...

The NamedQueries class will not be imported by default, add the


import by pressing Alt-Enter when the prompt appears.

Created by PTS APAC 16


5. To implement new named queries in the session bean, you could right
click LoanAppFacadeBean and choose Edit Session Facade. In this
case, you will add some further business logic to the method, so
instead, select LoanAppFacadeBean in the navigator and in the
structure pane, right click Methods and choose New.

6. In the Bean Method Details dialog, name the method


getCreditRating, return java.lang.Integer, and specify String ssn as
the parameter. Click OK to add the method to the interface and bean.

Created by PTS APAC 17


7. In LoanAppFacadeBean.java, implement the method as follows
public Integer getCreditRating(String ssn) {
LoanCustomer lc = (LoanCustomer)
em.createNamedQuery("LoanCustomer.findBySSN").setParameter("ss
n", ssn).getSingleResult();
return lc.getCreditRating().intValue();
}

8. Right-click the SOAServices project and choose the Make option to


compile your project. Verify that there are no errors reported in the
message log window.

Created by PTS APAC 18


9. Right click and choose Run to run the bean. You should see an
Embedded OC4J startup time in the Running log:
[TopLink Info]: 2005.12.27 04:08:24.144--ServerSession(2862)--
Thread(Thread[OC4J
Restarter,5,RequestThreadGroup])--current-workspaceapp_
LoanApp_Lab1_BusinessServices_0 login successful
Ready message received from Oc4jNotifier.
Embedded OC4J startup time: 11917 ms.

10. To call the method from the client, double click


LoanAppFacadeClient.java in the navigator, and add the following
code to the end of the try block in the main method:
String ssn = "123-12-1234";
System.out.println(loanAppFacade.getCreditRating(ssn));

11. Right click LoanAppFacadeClient.java and choose Run. Ensure that


the credit rating for the customer you specified is being returned.

Created by PTS APAC 19


Created by PTS APAC 20

You might also like