Yakura has posted 10 posts at DZone. View Full User Profile

OpenJPA Introductory Tutorial

02.05.2010
| 16934 views |
  • submit to reddit

Introduction

OpenJPA is an open source implementation of the Java JPA (Java Persistence API) specification from Apache. JPA provides an agnostic Java-based API for storing and retrieving information to a backend database. It has a canonical query language named Java Persistence Query Language, or JPQL, that blends with the programming methods of Java and eliminates the need to tailor database queries for a particular database. However, JPA also supports native SQL which can be used for quick ports with a known backend database. This tutorial is designed to walk you through the steps of setting up a simple web application to use OpenJPA Geronimo and to transact the derby database that comes with Geronimo. The tutorial code uses a simple Java Server Page (JSP), backed up by some basic classes. It displays a table of inventory items and categories. In this tutorial, we will not dive into details regarding the JSP code. Its purpose is to be a window through which you can examine OpenJPA.  The intended audience for this tutorial is those with some knowledge and understanding of the Java programming language and who are just beginning with OpenJPA. To start, you must download the following requirements and install them on your computer. For the purposes of this tutorial, we are using Eclipse as the IDE and Microsoft Windows as the operating system of choice.

Prerequisites

Geronimo V2.2: You can get it here. Download this file and unzip it to a permanent location. There is no installer. The server will run from the command line.

Java (J2SE) V1.6: This tutorial was developed and tested with Java V1.6. If you don't already have Java V1.6 you can get the IBM JDK here or the Sun JDK here.

Eclipse V3.2 or later: This version has annotation support included. Annotations play a large role in OpenJPA.  Download Eclipse 3.2 or later.

Apache OpenJPA library: For the purpose of implementing this tutorial you can select
OpenJPA v1.2 or greater. You can download Apache OpenJPA from the Apache site. Note that the Milestone (openjpa-all-2.0.0-M3.jar as of this writing) is an early release of OpenJPA 2.0 and may have some instabilities. No issues have been noted for the usage in this tutorial.

The tutorial code files: These files are provided with this tutorial. You will add them to your Eclipse project.

Setup and Running the Sample

Now, that you have all the prerequisites for this tutorial downloaded and installed, the following sections will walk you through the Eclipse project setup and the OpenJPA configuration. Make sure you read through and follow each part carefully. 

Setting Up Eclipse

After installing Eclipse, create a new project in a dedicated workspace for the tutorial. Complete the following setup instructions: First, make sure your Eclipse environment is updated and has the Geronimo plugin. If you do not know how to do that, follow the instructions found at the Geronimo website.

1.) Create a new Java project in Eclipse called, "OpenJPATutorial".

From the menu, select: File->New->Enterprise Application Project. (If Enterprise Application Project is not available as an option, select Project and then choose Enterprise Application Project from the list. Then click on the Next button).

When the New Project settings dialog appears, use the following settings:

2.) Under the Target Runtime section, select Apache Geronimo v2.2.

If you do not already have Geronimo setup in Eclipse then you will have to do so now. Click on the New... button.

If Apache Geronimo v2.2 does not appear in the list under Apache, click the Download additional server adapters link at the top right of the dialog. If the adapter does not appear in that list then follow the directions from the Geronimo site

Select Apache->Apache Geronimo v2.2.

Click Next.

Set the JRE to jre6 if it is not already set.

Browse for the install directory of Geronimo v2.2 on your system.

Click Finish. You should see the following:  

3.) Now, click the Next button. You should see this: 

Check the Generate application.xml deployment descriptor option.

Click the New Module... button:

De-select Create default modules.

Select the Web option.

Click Next

Click Finish. You will see the following:

Click Finish.

4.) Now, your Project Explorer should look like this (particular expanded): 

If you double-click on the Deployment Descriptor: OpenJPATutorial, you should see the application.xml open:

5.) Now we will bring in the sample code. The easiest way to add the sample code is to find the source provided with this tutorial and copy it to the src folder under the OpenJPATutorialWeb folder in your project directory in Windows Explorer: 

Now go back to Eclipse. Right-click on the OpenJPATutorialWeb folder in the Project Explorer view and select Refresh, or press the F5 key on your keyboard. Now you will see this: 

Notice that all the source files compile without error. That is because Geronimo comes with OpenJPA v1.1 built in.

6.) Now copy the index.jsp file from the tutorial into the Web Content directory under the Project directory in Windows Explorer:

Got to the Project Explorer and refresh the project. You should see this:

References
AttachmentSize
dynamicwebproj.jpg48.62 KB
enterprise app.jpg24.88 KB
projectexploere.jpg26.85 KB
deploymentdescr.jpg22.59 KB
openjpatut.jpg6.96 KB
openjpatutrefresh.jpg28.83 KB
image06.jpg8.5 KB
image062.jpg22.46 KB
image01.jpg11.21 KB
image02.jpg70.02 KB
image03.jpg64.79 KB
image04.jpg5.77 KB
image05.jpg21.08 KB
image06.jpg25.38 KB
image07.jpg37.19 KB
image08.jpg24.55 KB
image09.jpg7.41 KB
image10.jpg14.18 KB
image11.jpg18.69 KB
image12.jpg6.28 KB
image13.jpg36.59 KB
image020.jpg37.19 KB
image021.jpg24.55 KB
image022.jpg7.41 KB
image023.jpg14.18 KB
image024.jpg18.69 KB
image025.jpg6.28 KB
image026.jpg36.59 KB
image027.jpg17.25 KB
Published at DZone with permission of its author, Yakura Coffee. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Bill Fly replied on Mon, 2010/02/08 - 11:21am

It would be a better article if the figures all worked.

Gene De Lisa replied on Wed, 2010/02/24 - 8:00am

One of the worst articles I've seen here.

Bala Nagendra Betha replied on Fri, 2012/11/23 - 2:24am

Donot be so harsh on him.

I found the article useful. Perhaps the best so far I got any on JPA.

However, where are the table DDL scripts?

Without which I am not able to test the application.

Thanks once again.


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.