JPA is the Java API for the management of persistence and object/relational mapping. It is used in both Java EE 5 environments as well as in standalone Java SE environments. It provides an object/relational mapping facility using the Java domain model to manage relational databases. It enables a POJO-based programming model using Java annotations or XML file configurations.
JPA provides both a standard mapping definition (through annotations or XML) and a standard runtime API and object-level query language (JPQL). JPA is part of the EJB specification and Java EE 5 platform, but can be used in Java SE environments as well. The persistence API and the query language are supported under Enterprise JavaBeans 3.0. The JPA specification is being worked out as part of JSR-220 under the Java Community Process Program.
JPA significantly reduces the programming effort required to develop the persistence part of an application, because it allows a POJO based programming model with Java objects, and separates complex boiler-plate code from the application logic. The Java applications that connect to RDBMS data stores through JDBC™ APIs required you to develop a great deal of code for connection management, transaction management, queries, and the data retrieval through result sets. Usually, all this code is intertwined with application logic, making it cumbersome for application maintenance. With JPA, all this boiler-plate code is delegated to the JPA provider through annotations or XML files, leaving a clean application logic. JPA provides:
- Standard mapping definition (through annotations or XML)
- Standard runtime API
- Object-level query language (JPQL)
WebSphere Application Server, Community Edition v2.1 is a lightweight application server that is based on the Apache Geronimo v2.1 release. Henceforth, the WebSphere Application Server, Community Edition v2.1 is called simply Community Edition in the tutorial. Community Edition also includes many other defect fixes, and receives world-class support from IBM. You can download the binary images of the server for no charge. It is a fully certified server for Java EE 5 Apache Geronimo v2.1, hence Community Edition, uses OpenJPA for its JPA implementation.
OpenJPA is the Java EE persistence project of the Apache Software Foundation. It is an open source implementation of the persistence part of Enterprise Java Beans 3.0 specification. It provides a feature-rich implementation of JPA that can be used as a standalone POJO persistence layer, or integrated into any EJB 3.0 compliant container. Its many custom configurations mean you can use it in many lightweight frameworks with ease.
This tutorial shows you how to use JPA in Java EE 5 applications targeted to run on Community Edition. We illustrate various aspects of JPA as we go through the development of a Web application. We use the Eclipse integrated development environment (IDE) with the Community Edition plug-in for Eclipse (also called the Web Tools Platform (WTP) server adapter), to develop, deploy and run the application. This tutorial is for developers who want to quickly learn how use JPA as the persistence layer for their applications. The tutorial includes the following sections:
- Creating JPA projects in the Eclipse IDE and generating persistent classes
- Configuring persistence units through persistence.xml
- Packaging persistence units in the Web application
- Configuring persistence units in the Community Edition deployment plan
- Deploying the Web application
- Running the Web application
You should know database concepts and should be fairly skilled in Java programming. Understanding Java EE 5 concepts and annotations will help you with the context of the tutorial. You should have experience using an IDE like Eclipse. Since we will manually modify the XML configuration files (we do some bare-handed programming!!), knowledge of how deployment plans are written for an application server is indispensable.
To develop, deploy and run the application, the following environment is required:
- IBM Java SDK v1.5.0 SR7 or above
- Eclipse v3.3.1 with WTP v2.0.1
- WTP server adapter for Community Edition v2.1
- Community Edition v2.1
- DB2 Express v9.5
We will use two databases; the
database running in DB2-Express v9.5 and the
ACCOUNTDB running in the Derby database
embedded in Community Edition. We illustrate local transactions with
JPA using the
SAMPLE database, but we use
both the databases for illustrating JTA transactions.