Begin using OpenJPA - The BasicsPage edited by Scott Matthew QuintIntroductionOpenJPA 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. PrerequisitesGeronimo 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. DownloadEclipse 3.2 or later. Apache OpenJPA library: For the purpose of implementing this tutorial you can select The tutorial code files: These files are provided with this tutorial. You will add them to your Eclipse project. Setup and Running the SampleNow, 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 EclipseAfter 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.
Running and Configuring Geronimo and DerbyGeronimo has no installer and runs from the command line. Here are some quick instructions to get you started.
InventoryCategory.java package tutorial; import java.util.List; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.Version; @Entity public class InventoryCategory { private int version; private int id; private String categoryName; private String categoryDescription; List<InventoryItem> items; public InventoryCategory(){} @Column(name = "categoryName") public String getCategoryName() { return categoryName; } public void setCategoryName(String name) { this.categoryName = name; } @Column(name = "itemDescription") public String getCategoryDescription() { return categoryDescription; } public void setCategoryDescription(String description) { this.categoryDescription = description; } @Version @Column(name = "version_field") // not required public int getVersion() { return version; } public void setVersion(int version) { this.version = version; } @Id @GeneratedValue(strategy = GenerationType.AUTO) public int getId() { return id; } public void setId(int id) { this.id = id; } @OneToMany(targetEntity=tutorial.InventoryItem.class, cascade=CascadeType.ALL, mappedBy="category") public List<InventoryItem> getItems() { return items; } public void setItems(List<InventoryItem> items) { this.items = items; } public void addItem(InventoryItem item) { this.items.add(item); } }
SummaryThis was a very basic example of how to use OpenJPA with Geronimo and Derby. However, many applications that require database persistence do not use much more than the basic functionality demonstrated in this tutorial. The purpose of this was to be a primer. Aside from the setup of the server and database, we went through the creation of a persistence.xml file, the basics of the OpenJPA Entity, and EntityManager and some of the functionality.
*To make these changes you may have to delete the existing database tables so that they can recreated with the new relationship fields. References
Change Notification Preferences
View Online
|
View Change
|
Add Comment
|
- [CONF] OpenJPA > Begin using OpenJPA - The Basics confluence
- [CONF] OpenJPA > Begin using OpenJPA - The Basics confluence
- [CONF] OpenJPA > Begin using OpenJPA - The Basics confluence
- [CONF] OpenJPA > Begin using OpenJPA - The Basics confluence
- [CONF] OpenJPA > Begin using OpenJPA - The Basics confluence
- [CONF] OpenJPA > Begin using OpenJPA - The Basics confluence
- [CONF] OpenJPA > Begin using OpenJPA - The Basics confluence
- [CONF] OpenJPA > Begin using OpenJPA - The Basics confluence
- [CONF] OpenJPA > Begin using OpenJPA - The Basics confluence
- [CONF] OpenJPA > Begin using OpenJPA - The Basics confluence
- [CONF] OpenJPA > Begin using OpenJPA - The Basics confluence
- [CONF] OpenJPA > Begin using OpenJPA - The Basics confluence
- [CONF] OpenJPA > Begin using OpenJPA - The Basics confluence
- [CONF] OpenJPA > Begin using OpenJPA - The Basics confluence
- [CONF] OpenJPA > Begin using OpenJPA - The Basics confluence
- [CONF] OpenJPA > Begin using OpenJPA - The Basics confluence
- [CONF] OpenJPA > Begin using OpenJPA - The Basics confluence
- [CONF] OpenJPA > Begin using OpenJPA - The Basics confluence
- [CONF] OpenJPA > Begin using OpenJPA - The Basics confluence
