Persistence is critical for most enterprise applications because they require access to relational databases (such as Oracle Database 10g). If you are using a Java development application, you may need to complete some common tasks, such as database updates and retrieval, by writing JDBC and SQL. In recent years, several object-relational (O-R) mapping frameworks (such as Oracle TopLink, JBoss hibernate, and Bea Kodo) have become popular because they simplify persistence issues, freeing Java developers from the work of writing JDBC code, So that they can focus on the business logic. Some Java standards, such as the EJB 2.x container Management persistence (CMP) entity Bean, also attempt to address persistent challenges, but are less successful.
There are a number of options for building application persistence tiers, but there is not a single persistence standard for the Java platform that can be used in 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 specification JSR-220, standardizes the persistence API for the Java platform. JSR-220 is widely accepted by O-R mapping vendors (such as TopLink, Hibernate, and Kodo), as well as other leading application server vendors and JDO vendors. The EJB3 specification provides an attractive choice for building a persistent layer of enterprise Java applications.
In this article, I'll introduce the EJB3 Java persistence API, and I'll use a simple domain object model as an example.
Domain model
When building an enterprise application, we first design the domain object model that you want to save in the database, and then work with the Database Designer to determine the database schema. A domain model represents a persistent object or entity. Entities can be people, places, or things, and you store data about them. It contains data and behavior. The rich-domain model has all the object-oriented behavior characteristics, such as inheritance and polymorphism.
Our simple domain model (Figure 1) has a bidirectional one-to-many relationship between the department and the employee entity. Fulltime and contractor entities inherit from the employee entity.
Figure 1. Sample Domain object model
O-r Framework and EJB3 JPA Basics
If you build an application persistence layer using the O-R Mapping Framework (Oracle TopLink, for example), you will notice that each framework provides three kinds of tools:
A way to perform a o-r mapping declaratively. This method, called O-r mapping metadata, allows an object to be mapped to one or more database tables. Typically, most o-r frameworks use XML storage to map metadata o-r.
An API for manipulating entities (for example, performing CRUD operations). This API allows you to persist, retrieve, update, or remove objects. Based on the use of APIs and o-r mapping metadata, the O-R framework performs database operations on behalf of developers. This API frees developers from the task of writing JDBC or SQL code to persist domain objects.
A query language used to retrieve objects. This is the most important aspect of persistence, because illegal SQL statements can degrade the speed of the database. This approach also masks proprietary SQL that is cluttered with applications across the application. The query language allows you to retrieve entities or objects and liberate developers from the work of writing SQL SELECT statements.
The EJB3 Java Persistence API (JPA) provides a standard O-R mapping mechanism, a entitymanager API for performing crud operations, and a way to extend EJB-QL to retrieve entities, thus standardizing the persistence-oriented use of the Java platform. I will discuss these three aspects later.
Enable meta-data annotations
Java SE 5.0 introduces Meta data annotations. All components of Java EE, including EJB3 JPA, use large amounts of metadata annotations to simplify enterprise Java development. To learn more about metadata annotations, see the Bridging the GAP:J2SE 5.0 annotations of Kyle Downey. In EJB3 JPA, annotations can be used to define the injection of objects, relationships, o-r mappings, and persistence contexts. JPA also provides choices that use XML descriptors instead. I will mainly introduce the use of metadata annotations because they greatly simplify development. However, you might prefer to use XML descriptors in a production deployment environment because they can be used to override annotations.