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

Introduction to OpenJPA

  • submit to reddit

OpenJPA is the open source implementation of the Java Persistence API (JPA). OK, so what's that? THAT is a way to persist information in Java by way of using a back-end database and not having to write a bunch of messy JDBC code. Why is this needed? Well maybe it's not. But unless you are writing calculator software, some output, settings or something has to be saved off. There are plenty of schemes for saving data from writing flat text files, to elegant multi-megabyte XML streams to JDBC connected databases. But JPA is not just another scheme. For Java, it is really THE way to persist data. Why? Because it is the Java way.

During the execution of any interactive code, there is data that is kept. We have vectors and maps and sets and lists of custom classes that we can use to organize and browse information. Such constructs are really custom databases that exist only during program execution. Unless you save that data in some way, the data is lost. But to save it, we have to descend into some very inelegant code where we reformat the information for archiving. The reformatting pre-assumes the nature of the persistence. That means we have to know the JDBC database or we have to design the XML or text format. And then there has to be extra classes that serve no purpose other that to reformat the data. But what if you could add a couple lines of code to your existing data collection managers and the data could be automatically saved away to the database? JPA provides such capability. It's not perfect, of course, but once set up it makes saving data to a database very easy.

So if I haven't lost you by talking about execution time databases and collection managers, then you know what I am talking about and you share my pain. Good. I have written thousands of lines of code to read/write text files, parse DOM trees and transact databases in both ODBC and JDBC. It is never easy and it is never fun. But it's almost always necessary and usually makes an otherwise clean implementation very heavy with messy code. So when I was presented with OpenJPA, I was very interested.

However, when I started looking around for examples or tutorials that spoke in simple terms, all I found was frustration. Once JPA is set up, it's fairly easy to code with it. But knowing what has to be set up and how is the tricky part. Doing it is less tricky if you know what to do but finding that knowledge is not as straight forward as you'd think.

When trying to use a new API, I prefer to see an example of it being used in the simplest of cases. I can then adapt that example for my own use and continue on by referencing the documentation. But JPA is really designed for J2EE applications and that brings in a bunch of complexity. To see what the JPA code does I have to understand what the whole program is doing. Worse, I also have to have a runtime environment that matches the example.  Plan B: just read the specification. Yeah right....OK...plan C?

Well, after much frustration and losing all my spare change by bribing busy associates (who had some experience with JPA) with sodas and coffee, I figured it out. And, because I believe that OpenJPA is worthwhile and I have the discipline to do it, I have put together a couple tutorials that step through setting up and using OpenJPA in excruciating detail.

The first tutorial is a J2EE example. It's not very J2EE-ish because the point is to examine JPA. If you want to try JPA for free and not have to go through a bunch of different installs, this is the one for you. It uses Eclipse, Geronimo, Derby (which is built in to Geronimo) and OpenJPA. They are all free downloads and the tutorial walkes though how to set up and use everything. That's because I hate it when tutorials assume you have something already set up or require you to know something. And as a side benefit, after this you will be introduced to Eclipse, Geronimo, Derby and JSP.

The tutorial is hosted at the Apache OpenJPA website. Just click here. One caveat: the tutorial uses Geronimo 2.2. It looks like the Eclipse plugin for that version is still not generally available. SO, if you are really setting everything up from scratch, use Geronimo 2.1.3 or 2.1.4 instead.

In the interest of disclosure, OpenJPA is just one of many different JPA implementations. But OpenJPA is genuinely open and it is the preferred implementation of WebSphere, which is still the most used J2EE app server.

 Go to the tutorial.

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.)


Andy Jefferson replied on Wed, 2010/02/03 - 12:07pm

> OpenJPA is just one of many different JPA implementations. But OpenJPA is genuinely open


In the interest of more disclosure : there are indeed many JPA implementations, and *many* of them are genuinely open. To name some more ... DataNucleus (Apache 2 license), Hibernate (LGPL license). The fact that OpenJPA is the "preferred" implementation on WebSphere is presumably derived from the fact that many IBM people are committers on the OpenJPA project and WebSphere is an IBM project, not due to any inherent technical superiority specific to WebSphere.

Rob Wisniewski replied on Fri, 2010/02/05 - 10:27am

Andy.. I don't think the implication is that it was the ONLY open implementation, only that it stands at the nexus of an open implementation as well as the basis of a long standing and popular Java Enterprise software stack. IBM has a lot invested, and that goes a long way when making choices and climbing the learning curve.

Piero Sartini replied on Sat, 2010/02/06 - 6:45am

Just to name another long standing, popular and genuinely open implementation: EclipseLink. Long time known as TopLink and sold by Oracle, it now is completely free and became the reference implementation for JPA.

I like OpenJPA and Hibernate and hope that they get a stable JPA2 implementation out of the gate soon. It has some real nice additions I can't miss anymore. I am aware of 2 implementations right now: EclipseLink and DataNucleus(which supports JDO as well).

Comment viewing options

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