ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Standardizing Java Persistence with the EJB3 Java Persistence API

by Debu Panda
05/17/2006

Persistence is critical for most enterprise applications because they require access to relational databases such as Oracle Database 10g. If you're developing applications using Java, you're probably responsible for mundane tasks such as database updates and retrieval, which you do by writing JDBC and SQL. Over the last few years, several object-relational (O-R) mapping frameworks such as Oracle TopLink and JBoss Hibernate have become popular because they make persistence challenges simpler, freeing Java developers to write JDBC code and let them concentrate on business logic. Java standards such as EJB 2.x container-managed persistence (CMP) entity beans have also tried to solve persistence challenges but have been less successful.

Although there are several options for building the persistence tier of applications, there is no single persistence standard for the Java platform that can be used in both the Java EE and Java SE environments. The good news is that the EJB3 Java Persistence API (JPA), which is part of the EJB 3.0 spec (JSR-220), standardizes persistence API for the Java platform. JSR-220 was widely received by O-R mapping vendors such as TopLink and Hibernate, as well as other leading application server vendors and JDO vendors. The EJB3 specification offers a compelling option for building the persistence tier of enterprise Java applications.

In this article, I'll introduce the EJB3 Java Persistence API by using a simple domain object model as an example.

The Domain Model

When you build an enterprise application, you first design the domain object model that you want to persist in the database; then, you work with database designers to come up with the database schema. The domain model is the representation of persistent objects or entities. An entity is a person, place, or thing about which you store data. It contains both data and behavior. A rich domain model has the characteristics of all object-oriented behavior, such as inheritance and polymorphism.

Our simple domain model (Figure 1) has a bidirectional one-to-many relationship between Department and Employee entities. The FullTime and Contractor entities inherit from the Employee entity.

Figure 1
Figure 1. Sample domain object model

The Basics of the O-R Framework and EJB3 JPA

If you've used an O-R mapping framework such as Oracle TopLink to build the persistence tier of your application, you'll notice that each framework provides three facilities:

  1. A declarative way to perform O-R mapping. This method, called O-R mapping metadata, lets you map an object to one or more database tables. Typically most O-R frameworks use XML for storing O-R mapping metadata.
  2. An API to manipulate entities (for example, to perform CRUD operations). The API lets you persist, retrieve, update, or remove objects. Based on the use of the API and the O-R mapping metadata, the O-R framework performs database operations on your behalf. The API shields you from writing JDBC or SQL code to persist your domain objects.
  3. A query language to retrieve objects. This is one of the most important aspects of persistence because improper SQL statements may slow down your database. This approach also shields your application from proprietary SQL being cluttered all over your applications. A query language lets you retrieve entities or objects and spares you from writing SQL SELECT statements.

The EJB3 Java Persistence API (JPA) standardizes the use of persistence for the Java platform by providing a standard mechanism for O-R mapping, an EntityManager API to perform CRUD operations, and a way to extend EJB-QL to retrieve entities. I'll discuss these three aspects of JPA later.

Metadata Annotation in Action

Java SE 5.0 introduced metadata annotations. All components of Java EE, including EJB3 JPA, heavily use metadata annotations to simplify enterprise Java development. To learn more about metadata annotations, see Bridging the Gap: J2SE 5.0 Annotations by Kyle Downey. In EJB3 JPA, annotations may be used to define objects, relationships, O-R mapping, and the injection of persistent contexts. JPA also provides an option to use XML descriptors instead. I'll primarily focus on using metadata annotations because they make development much simpler. However, you may prefer to use XML descriptors in a production deployment environment because you can use them to override annotations.

Pages: 1, 2, 3, 4, 5

Next Pagearrow