iBATIS, Hibernate, and JPA: Which is right for you?

Object-relational mapping solutions compared

1 2 3 4 5 6 7 Page 2
Page 2 of 7

Object-relational mapping

Object-relational mapping (ORM) has emerged as a solution to what is sometimes called the object-relational impedance mismatch. ORM is a technique that transparently persists application objects to the tables in a relational database. ORM behaves like a virtual database, hiding the underlying database architecture from the user. ORM provides functionality to perform complete CRUD operations and encourages object-oriented querying. ORM also supports metadata mapping and helps in the transaction management of the application.

An example will help illustrate how ORM works. Consider a simple Car object that needs to be persisted in the database. The Car object in the domain model is the representation of the CAR table in the data model. The attributes of the Car object are derived from the columns of the CAR table. There is a direct mapping between the Car class and the CAR table, as illustrated in Figure 1.

Mapping an object to a table
Figure 1. Mapping an object to a table

There are many open source ORM tools, including Hibernate, iBATIS SQL Maps, and Java Ultra-Lite Persistence. Most of these tools are persistence frameworks that provide a layer of abstraction between the Java application and the database. A persistence framework maps the objects in the application domain to data that needs to be persisted in a database. The mappings can be defined using either XML files or metadata annotations (the latter introduced to the language as part of Java 1.5). The persistence framework aims to separate the database-related code and the application code (that is, the business logic), thereby increasing application flexibility. A persistence framework simplifies the development process by providing a wrapper around the persistence logic.

With this basic introduction to persistence out of the way, we're ready to move on to discussing two of the most popular open source persistence frameworks, iBATIS and Hibernate. We'll also introduce the Java Persistence API and discuss the strengths and weaknesses of all three solutions in various application scenarios.

iBATIS: Using SQL directly

Object-relational mapping (ORM) uses direct mapping to generate JDBC or SQL code under the hood. For some application scenarios, however, you will need more direct control over SQL queries. When writing an application that involves a series of update queries, it's more effective to write your own SQL queries than to rely on ORM-generated SQL. Also, ORM cannot be used when there is a mismatch between the object model and the data model. As we've mentioned, JDBC code was once the common solution to such problems, but it introduced a lot of database code within application code, making applications harder to maintain. A persistence layer is needed to decouple the application and the database.

iBATIS in brief

The iBATIS project was initiated by Clinton Begin and released in 2001. This persistence framework was initially designed for Java, though it has since been extended to support other platforms, including .Net and Ruby.

The iBATIS Data Mapper framework helps solve these problems. iBATIS is a persistence framework that provides the benefits of SQL but avoids the complexity of JDBC. Unlike most other persistence frameworks, iBATIS encourages the direct use of SQL and ensures that all the benefits of SQL are not overridden by the framework itself.

Simplicity is iBATIS's greatest advantage, as it provides a simple mapping and API layer that can be used to build data-access code. In this framework the data model and the object model need not map to one another precisely. This is because iBATIS uses a data mapper, which maps objects to stored procedures, SQL statements, or ResultSets via an XML descriptor, rather than a metadata mapper, which maps objects in the domain to tables in the database. Thus, iBATIS enables the data model and the object model to be independent of each other.

How iBATIS works

iBATIS allows loose coupling of the database and application by mapping the input to and output from the database to the domain objects, thus introducing an abstraction layer. The mapping is done using XML files that contain SQL queries. This loose coupling allows the mapping to work for systems where the application and the database design are mismatched. It also helps in dealing with legacy databases and with databases that change over time.

The iBATIS framework mainly uses the following two XML files as descriptors:

  • SQLMapConfig.xml
  • SQLMap.xml

We'll look at each file in detail.


SQLMapConfig.xml is a central XML file that contains all the configuration details, like the details for the data sources; it also optionally includes information about transaction management. This file identifies all the SQLMap.xml files -- of which there may be more than one -- and loads them.

Consider an Employee class that maps to an EMPLOYEE table in the database. The properties of the class -- emp_id, emp_firstname, and emp_lastname -- correspond to similarly named columns in the table. The class diagram for the Employee class is shown in Figure 2. (This class will be used to demonstrate the different persistence techniques that are discussed in this article.)

Class diagram for the Employee class
Figure 2. Class diagram for the Employee class

The SQLMapConfig.xml file for the Employee class can be written as shown in Listing 1.

Listing 1. SQLMapConfig.xml file for Employee

  <transactionManager type="JDBC" commitRequired="false">
    <dataSource type="EMPLOYEE">
     <property name="JDBC.Driver"           value="com.mysql.jdbc.Driver"/>
      <property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost:3306/ibatis"/>
      <property name="JDBC.Username" value="root"/>
      <property name="JDBC.Password" value=""/>
 <!-- List the SQL Map XML files. They can be loaded from the classpath, as they are here (com.mydomain.data...) -->
  <sqlMap resource="com/mydomain/data/Employee.xml"/>

SQLMapConfig.xml uses a transactionManager tag to configure a data source to use with this particular SQL map. It specifies the type of the data source, along with some details, including information about the driver, the database URL, and the username and password. The sqlMap tag specifies the location of the SQLMap.xml file so as to load it.

1 2 3 4 5 6 7 Page 2
Page 2 of 7