Understanding JPA, Part 1: The object-oriented paradigm of data persistence

Build a wall between data persistence and your Java code

Print Email Feedback Resources Discuss
Digg Reddit SlashDot Stumble del.icio.us Technorati dzone

Numerous projects have sought to make the data persistence layer a good object-oriented citizen in Java application development. The Java Persistence API, an outgrowth of JSR 220, collects and standardizes that work. In this first half of a two-part introduction to the Java Persistence API, learn how JPA tames the process of writing code that manipulates data, making it a more natural piece of your object-oriented architecture.

Why JPA?
A fundamental question for many Java developers is "Why JPA? Why do I need to know how to use this API when object-relational mapping tools like Hibernate and Toplink are already available?" The answer is that JPA is not a new technology; rather, it has collected the best ideas from existing persistence technologies like Hibernate, TopLink, and JDO. The result is a standardized specification that helps you build a persistence layer that is independent of any particular persistence provider.

Like it or not, data is an integral part of any application, even cool object-oriented ones. Everything stops at the persistence layer, where Java developers traditionally have had to write complex SQL queries, which can become unmanageable as the application grows. How good it would be if you could just treat these queries as objects, and apply object-oriented programming concepts like encapsulation, abstraction, inheritance, and polymorphism to them! With such an abstraction, you could hide the complexity of the underlying data store.

In fact, the Java community has produced numerous object-oriented approaches to data persistence: EJB, JDO, Hibernate, and Toplink are all worthy solutions that have tackled this problem. The Java Persistence API, or JPA, is a standard persistence API introduced as part of the Java EE 5 platform. The JPA specification was first introduced as part of JSR 220: EJB 3.0, with the goal of simplifying the EJB entity beans programming model. Although it all started with entity beans and is packaged with Java EE 5.0, JPA can be used outside the container in a Java SE environment.

In this article, you will see how elegantly data persistence can be handled in an object-oriented manner just with the help of JPA annotations. The article is intended for readers new to JPA who understand the basic concepts of relational database management systems and are familiar with Java 5 annotations. JPA requires Java 5 or higher, as it makes heavy use of new Java language features such as annotations and generics.

OpenJPA and the example application

Concepts in this article are explained and demonstrated using OpenJPA, an open source JPA implementation from Apache. I chose OpenJPA over other vendor product because it has been integrated with the Weblogic, WebSphere, and Geronimo application servers. The current version of OpenJPA at the time of writing is 1.0.1; there's a link to the binary distribution in the Resources section. If you're going to use a different setup, obviously you'll need to check out the documentation first.

The remainder of this article will explore various object-oriented JPA concepts with a sample application that implements functionality for a simple development scenario. Imagine there's a superstore called XYZ with both an online and retail customer base. To begin, you will see how to apply CRUD operations to this customer model using JPA. In the later sections, you'll see how you can extend the CRUD operations using object inheritance -- the JPA way.

The code package for this article includes sample code for entity listeners and all three inheritance types (single table, joined, and table per class) discussed in the article, implemented in the context of the example application.

Print Email Feedback Resources
Digg Reddit SlashDot Stumble del.icio.us Technorati dzone
Comments (27)
27
Welcome, Logout
Login
Forgot your account info?

Excellent piece of infoBy agnibemsr6 on March 14, 2010, 10:06 pmThis article explains JPA annotations in a way that even a newbie like me can understand the entire thing well. :)

Reply | Read entire comment

JPA annotationsBy Anonymous on March 10, 2010, 9:47 amExcellent post on the benefits of JPA annotations. Thank you. regcure review

Reply | Read entire comment

Listing 19. A sample superclass in table per class inheritance..By Anonymous on March 9, 2010, 4:59 amListing 19. A sample superclass in table per class inheritance...we got to use generatorType.Table instead of auto

Reply | Read entire comment

Very basicBy Anonymous on February 26, 2010, 10:45 amVery basic

Reply | Read entire comment

It's GreatBy Anonymous on February 12, 2010, 5:43 pmThans!!! l.o.l.;

Reply | Read entire comment

View all comments

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Archived Discussions (Read only)
Resources
Download the example application source code that accompanies this article. Learn more about the Java Persistence API, a standardized object-relational mapping mechanism for the Java platform. EJB, JDO, Hibernate, and Toplink all offer their own take on object-oriented data access. "Persist data with Java Data Objects" (Jacek Kruszelnicki, JavaWorld, March 2002) is a first look at JDO. "Querying for persistent objects without a query language" (Gerald Bachlmayr, JavaWorld, March 2006) presents an advanced implementation of Hibernate combined with JavaServer Faces. See "Simplify enterprise Java development with EJB 3.0" (Michael Juntao Yuan, JavaWorld, August 2005) for an introduction to EJB 3.0's annotations-driven programming model. The Java Persistence API arose as part of the work of the JSR 220: EJB 3.0 expert group. Download OpenJPA from the Apache Foundation. See Sun's Java persistence blueprints for more about using JPA. Find out more about the the Java Persistence API Implementation at GlassFish. Visit the JavaWorld Java Standard Edition research center for more articles about core Java concepts, including data access. Check out the JavaWorld developer forums for discussions and Q&A on data access and more. Also see Network World's IT Buyer's Guides: Side-by-side comparison of hundreds of products in over 70 categories.



You are viewing a mobilized version of this site...
View original page here

Mobilized by Mowser Mowser