Java: A platform for platforms
Sun's reorg may seem promising to shareholders but it's also a scramble for position. The question now is whether Sun can, or wants to, maintain its hold on Java technology. Especially with enterprise leaders like SpringSource and RedHat investing heavily in Java's future as a platform for platforms

Also see:

Discuss: Java: A platform for platforms?

Featured Whitepapers
Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Understanding JPA, Part 2: Relationships the JPA way

Handle data relationships with object-oriented grace

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Your Java applications are dependent on a web of data relationships, which can become a tangled mess if improperly handled. In this second half of her introduction to the Java Persistence API, Aditi Das shows you how JPA uses annotations to create a more transparent interface between object-oriented code and relational data. The resulting data relationships are easier to manage and more compatible with the object-oriented programming paradigm.

Data is an integral part of any application; equally important are the relationships between different pieces of data. Relational databases support a number of different types of relationships between tables, all designed to enforce referential integrity.

In this second half of Understanding JPA, you will learn how to use the Java Persistence API and Java 5 annotations to handle data relationships in an object-oriented manner. This article is meant for readers who understand basic JPA concepts and the issues involved in relational database programming in general, and who would like to further explore the object-oriented world of JPA relationships. For an introduction to JPA, see "Understanding JPA, Part 1: The object-oriented paradigm of data persistence."

A real-life scenario

Imagine a company called XYZ that offers five subscription products to its customers: A, B, C, D, and E. Customers are free to order products in combination (at a reduced price) or they can order individual products. A customer need not pay anything at the time of ordering; at the end of the month, if the customer is satisfied with the product, an invoice is generated and sent to the customer for billing. The data model for this company is shown in Figure 1. A customer can have zero or more orders, and each order can be associated with one or more products. For each order, an invoice is generated for billing.

Diagram of a data model

Figure 1. A sample data model (click to enlarge)

Now XYZ wants to survey its customers to see how satisfied they are with its products, and hence needs to find out how many products each customer has. In order to figure out how to improve the quality of its products, the company also wants to conduct a special survey of those customers who cancelled their subscriptions within the first month.

Traditionally, you might tackle this problem by building a data access object (DAO) layer where you would write complex joins between CUSTOMER, ORDERS, ORDER_DETAIL, ORDER_INVOICE, and PRODUCT tables. Such a design would look good on the surface, but it might be hard to maintain and debug as the application grew in complexity.

JPA offers another, more elegant way to address this problem. The solution I present in this article takes an object-oriented approach and, thanks to JPA, doesn't involve creating any SQL queries. Persistence providers are left with the responsibility of doing the job transparently to the developers.

Before continuing, you should download the sample code package from the Resources section below. This includes sample code for the one-to-one, many-to-one, one-to-many, and many-to-many relationships explained in this article, in the context of the example application.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (1)
Login
Forgot your account info?

How to insert data?By Anonymous on October 27, 2008, 2:01 pmI was going thru the example that you have stated in your article "Understanding JPA" at JavaWorld. It was cleanly explained. One thing I was looking in your example...

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