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

Object-relational mapping solutions compared

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


Hibernate is an open source, lightweight object-relational mapping solution. The main feature of Hibernate is its support for object-based modeling, which allows it to provide a transparent mechanism for persistence. It uses XML to map a database to an application and supports fine-grained objects. The current version of Hibernate is 3.x, and it supports Java annotations and hence satisfies the EJB specification.

Hibernate in brief

Hibernate was developed by a team headed by Gavin King. The development of Hibernate began in 2001 and the team was later acquired by JBoss, which now manages it. Hibernate was developed initially for Java; in 2005 a .Net version named NHibernate was introduced.

Hibernate includes a very powerful query language called Hibernate Query Language, or HQL. HQL is very similar to SQL, and also defines some additional conventions. HQL is completely object-oriented, enabling you to leverage the complete strength of the object-oriented pillars of inheritance, polymorphism, and association. HQL queries are case insensitive, except for the names of the Java classes and properties being used. HQL returns query results as objects that can be directly accessed and manipulated by the programmer. HQL also supports many advanced features of pagination and dynamic profiling that SQL has never supported. HQL does not require any explicit joins when working with multiple tables.

Why do we need Hibernate?

Entity beans, which have traditionally been used for object-relational mapping, are very difficult to understand and hard to maintain. Hibernate makes object-relational mapping simple by mapping the metadata in an XML file that defines the table in the database that needs to be mapped to a particular class. In other persistence frameworks, you need to modify the application class to achieve object-relational mapping; this is not necessary in Hibernate.

With Hibernate, you needn't worry about database changes, as manual changes in the SQL script files are avoided. If you ever need to change the database your application uses, that can be easily accommodated by altering the dialect property in the configuration file. Hibernate gives you the complete power of SQL, something that was never offered by earlier commercial ORM frameworks. Hibernate also supports many databases, including MySQL, Oracle, Sybase, Derby, and PostgreSQL, and works well with plain old Java object (POJO)-based models, too.

Hibernate generates JDBC code based on the underlying database chosen, and so saves you the trouble of writing JDBC code. It also supports connection pooling. The APIs that are used by Hibernate are very simple and easy to learn. Developers with very little knowledge of SQL can make use of Hibernate, as it lessens the burden of writing SQL queries.

Hibernate architecture

Internally, Hibernate uses JDBC, which provides a layer of abstraction to the database, while it employs the Java Transaction API (JTA) and JNDI to integrate with other applications. The connection information that the Hibernate needs to interact with the database is provided by the JDBC connection pool, which has to be configured.

Hibernate's architecture consists mainly of two interfaces -- Session and Transaction -- along with the Query interface, which is in the persistence layer of the application. The classes that are defined in the business layer of the application interact through independent metadata with the Hibernate persistence layer, which in turn talks to the database layer using certain JDBC APIs. In addition, Hibernate uses other interfaces for configuration, mainly the aptly named Configuration class. Hibernate also makes use of callback interfaces and some optional interfaces for extending the mapping functionality. The overall Hibernate architecture is illustrated in Figure 3.

Hibernate architecture: The big picture
Figure 3. Hibernate architecture: The big picture

The major programming interfaces that are part of Hibernate are:

  • org.hibernate.SessionFactory is basically used to obtain a session instance, and can be seen as an analogue to the connection pooling mechanism. This is thread safe, as all the application threads can use a single SessionFactory (as long as Hibernate uses a single database). This interface is configured through the configuration file, which determines the mapping file to be loaded.

  • org.hibernate.Session provides a single thread that determines the conversation between the application and the database. This is analogous to a specific (single) connection. It is very lightweight and not thread safe.

  • org.hibernate.Transaction provides a single-thread object that spans through the application and determines an atomic unit of work. It basically abstracts JDBC, JTA, and CORBA transactions.

  • org.hibernate.Query is used to perform a query, either in HQL or in the SQL dialect of the underlying database. A Query instance is lightweight, and it is important to note that it cannot be used outside the session through which it was created.

Configuring Hibernate

You configure Hibernate through an XML file named hibernate.cfg.xml. The configuration file aids in establishing a connection to a particular relational database. The configuration file should know which mapping file it needs to refer to. At runtime, Hibernate reads the mapping file and then uses it to build a dynamic Java class corresponding to that table of the database. A sample configuration file is shown in Listing 6.

Listing 6. hibernate.cfg.xml

    <!-- local connection properties -->
     <property name="hibernate.connection.url">
     <property name="hibernate.connection.driver_class">
     <property  name="hibernate.connection.username">
     <property name="hibernate.connection.password">
     <!-- dialect for MySQL -->
     <property name="dialect">
     <property name="hibernate.show_sql">false</property>
     <property name="hibernate.transaction.factory_class">
     <mapping resource="Employee.hbm.xml" />
1 2 3 4 5 6 7 Page 4
Page 4 of 7
How to choose a low-code development platform