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

Object-relational mapping solutions compared

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

Working with Hibernate

When a SessionFactory instance is created in the application, Hibernate reads the configuration file and identifies the respective mapping file. The session object that is created from the SessionFactory gets a particular connection to the database, and this session object is the persistence context for the instance of a persistence class. The instance can be in one of the three states: transient, persistent, or detached. In the transient state, the object is yet to be associated with a table; in the persistent state, the object is associated with the table; and in the detached state, there is no guarantee that the object is in sync with the table. The Hibernate code that is used to persist an Employee object is shown in Listing 7.

Listing 7. Persisting an object with Hibernate

Session session = null;
Transaction tx = null;

// At this point the Configuration file is read
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

// A specific session object is obtained
session = sessionFactory.openSession();

// A new database transaction is started
tx = session.beginTransaction();

// Employee Object is created & populated
Employee emp = new Employee();
emp.setEmpFirstname("K L");

// Using the session, emp object is persisted in the database

The mapping file that the configuration file identifies maps a particular persistent class to the database table. It maps specific columns to specific fields, and has associations, collections, primary key mapping, and ID key generation mechanisms. The mapping files are generally given names based on the tables to which they map; in the example application, you'd use Employee.hbm.xml for the file that corresponds to the EMPLOYEE table. As you can see in Listing 8, the mapping file specifies that the Employee class has to be mapped to the EMPLOYEE table in the database, which has columns named id, emp_firstname, and emp_lastname. id is the primary key, and should have the value assigned.

Listing 8. Employee.hbm.xml

<hibernate-mapping package="demo">
  <class name="Employee" table="employee" >
    <meta attribute="sync-DAO">false</meta>
      <id name="Id" type="integer" column="emp_id">
        <generator class="assigned"/>
       <property name="EmpFirstname" column="emp_firstname"
              type="string" not-null="true" length="30" />
      <property name="EmpLastname" column="emp_lastname"                      type="string" not-null="true" length="30" />

When to use Hibernate

Hibernate is best used to leverage end-to-end OR mapping. It provides a complete ORM solution, but leaves you control over queries. Hibernate is an ideal solution for situations where you have complete control over both the application and the database design. In such cases you may modify the application to suit the database, or vice versa. In these cases you could use Hibernate to build a fully object-relational application. Hibernate is the best option for object-oriented programmers who are less familiar with SQL.

The Java Persistence API

Hibernate and JPA

Having just finished learning about how Hibernate can serve as a standalone persistence solution, you may be surprised to discover that it can also work with JPA. Strictly speaking, if you're going to use Hibernate by itself, you'll be using the Hibernate Core module, which generates SQL using HQL without the need for handling JDBC objects; the application is still independent of databases. Hibernate Core can be used with any application server, and for any generic Java application that needs to perform object-relational mapping. This mapping will is achieved by using native Hibernate APIs, the Hibernate Query Language, and XML mapping.

The Hibernate team was deeply involved in the development of the EJB 3 specification. After the introduction of EJB 3, a standalone implementation of EJB 3 persistence was made available as part of Hibernate -- Hibernate Annotations and Hibernate EntityManager. These two are built on top of Hibernate Core. For applications developed using Java EE 5 in which there is a need to use EJB 3, Hibernate EntityManager can be considered as an option for the persistence provider. Applications developed using Java EE 5 will utilize Hibernate and JPA working together.

The Java Persistence API is the standard object-relational mapping and persistence management interface for the Java EE 5 platform. As part of the EJB 3 specification effort, it is supported by all major Java vendors. The Java Persistence API draws on ideas from leading persistence frameworks and APIs, such as Hibernate, Oracle TopLink, Java Data Objects (JDO), and EJB container-managed persistence. JPA provides a platform on which specific implementations of persistence providers can be used. One of the main features of the Java Persistence API is that any persistence provider can be plugged in to it.

JPA is a POJO-based standard persistence model for ORM. It is part of the EJB 3 specification and replaces entity beans. The entity beans defined as part of the EJB 2.1 specification had failed to impress the industry as a complete persistence solution for several reasons:

  • Entity beans are heavyweight components and are tightly coupled to a Java EE server. This makes them less suitable than lightweight POJOs, which are more desirable for their reusability.
  • Entity beans are difficult to develop and deploy.
  • BMP entity beans force you to use JDBC, while CMP entity beans are highly dependent on the Java EE server for their configuration and ORM declaration. These restrictions will affect the performance of the application.

To address these issues, the EJB 3 software expert group developed JPA as part of JSR 220. JPA borrows the best ideas from other persistence technologies. It defines a standard persistence model for all Java applications. JPA can be used as the persistence solution for both Java SE and Java EE applications.

JPA uses metadata annotations and/or XML descriptor files to configure the mapping between Java objects in the application domain and tables in the relational database. JPA is a complete ORM solution and supports inheritance and polymorphism. It also defines an SQL-like query language, JPQL (Java Persistence Query Language), which is different from EJB-QL (EJB Query Language), the language used by entity beans.

With JPA, you can plug in any persistence provider that implements the JPA specification instead of using whatever default persistence provider comes with your Java EE container. For example, the GlassFish server uses TopLink Essentials, provided by Oracle, as its default persistence provider. But you could choose to use Hibernate as the persistence provider instead by including all the necessary JAR files in your application.

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