OpenJPA Introductory Tutorial
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.
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.
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 Finish. You will see the following:
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:
|enterprise app.jpg||24.88 KB|
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)