The Contenders
What is a Java persistence framework (also referred to as a Java object-relational mapping mechanism)? It's the part of a program that moves data in Java objects to and from a relational data store. There are several persistence frameworks. Of course, you can also write your own using the JDBC API.
The goal of these frameworks is to reduce the amount of Java code (in other words your own JDBC code) needed to get the Java object data in and out of the database. This article compares the following frameworks:
- Hibernate, Version 3: The very popular open source persistence framework
- JPOX JDO, Version 1.2: The reference implementation of Sun's persistence Java Data Objects 2.0 specification.
- Castor JDO, Version 1.0.5: An open source framework that provides Java-to-XML binding and Java-to-SQL persistence. (Side note: Even though JDO is in its name, Castor JDO is NOT a JDO implementation. To avoid confusion, I’ll refer to it simply as Castor in this article.)
- iBatis for Java, Version 2.2: Another open source, lightweight data persistence framework.
- JDBC: The JDBC API is provided with any standard Java Standard Edition download. Homegrown JDBC solutions provide a baseline for comparison for all frameworks.
A student of the Java persistence problem will recognize that this is not a complete list of options. There are several commercial persistence frameworks such as
Oracle's TopLink,
Thought Inc.'s CocoBase, and
ATG's SQL repository mechanisms. There are also other lesser-known open source frameworks. Many of these can be found at
www.java-source.net.
The Spring framework also offers JDBC abstraction although it "integrates out of the box" with a number of the frameworks examined here. And there is, of course,
Enterprise JavaBeans 3.0 and the relatively new
Java Persistence API that originated as part of the work of the JSR 220 (EJB 3.0). So, while there are other options, this article concentrates on those frameworks that are free (open source), relatively popular and do not require a Java Enterprise Edition (JEE) container.
Framework Acquisition and Setup
Each of the persistence frameworks is very simple to download and get setup. In fact, all of the frameworks here simply require adding the appropriate libraries to the build and execution paths. Most of these, as open source frameworks, require additional open source libraries for tasks such as XML parsing and logging. Except for Castor, the persistence framework download obtained from the project Web site will contain both the framework JAR as well as all the required libraries (except for the database driver). With Castor, you will have to make an extra visit to some additional projects' Web sites for required libraries. Table 1 provides an overview of what is downloaded and required for using each of the frameworks in your Java application.
|
Persistence Framework
|
Version
|
Download included libraries
|
Additional libraries needs
|
|
Hibernate
|
3.0
|
antlr.jar
cglib.jar
asm.jar
asm-attrs.jars
commons-collections.jar
commons-logging.jar
hibernate3.jar
jta.jar
dom4j.jar
ehcache.jar
c3po.jar
|
database
driver
|
|
Castor
|
1.0.5
|
castor-1.0.5.jar
|
commons-logging.jar
xercesImpl.jar
database
driver
|
|
JPOX JDO
|
1.2
|
jdo.jar
jpox.jar
log4j.jar
|
database driver
|
|
IBatis
|
2.2.0
|
ibatis-common-2.jar
ibatis-sqlmap-2.jar
|
database driver
|
|
JDBC
|
Java 1.5
|
JDBC API with
standard Java
|
database
driver
|
Table 1. What's required? An overview of what is downloaded and required for using each of the frameworks in your Java application.
IDE Considerations
One development consideration when building an application that uses any of the frameworks is to look at the support your favorite IDE gives in XML editing. It's common practice to configure applications now by XML and these persistence frameworks make heavy use of XML in object-relational mapping. Most of these IDEs allow you to create/edit an XML file, but the good ones also provide code assist based on the persistence framework's Document Type Definitions. Each of the frameworks configuration files are structured via DTD.
JDO is a little unusual in that it also requires an additional step before you can deploy/execute an applicationa special pre-deployment operation. After compiling your persistence-capable Java objects into the standard class byte codes (.class files), JDO requires you run these classes through a special JDO compilation called byte code enhancement. Below is an example of the JDO compile command that is completed on the persistent classes that result in modified byte code files:
java -cp C:\NetBeansProjects\Persistence\build\web\WEB-INF\classes;
c:\java\jpox\jpox-enhancer-1.1.4.jar;c:\java\jpox\jpox.jar;
c:\java\jpox\jdo.jar;c:\java\jpox\log4j.jar;c:\java\bcel-5.2\bcel-5.2.jar
-Dlog4j.configuration=file:log4j.properties org.jpox.enhancer.JPOXEnhancer
-v com\intertech\domain\package.jdo
Therefore, in addition to the libraries listed above, JDO also requires you (and your development environment) to use a JDO tool provided with the JDO libraries. In many cases, you'll want to create an Ant task to perform this activity.