Java and Hibernate: An Overview

You may have heard of the term Hibernate from various sources: coworkers, programmers, blogs, or perhaps in job listings and unless you’ve specifically done Hibernate-related work, what it is and how you can implement it in your own project may elude you.

So what is “Hibernate” anyway?

Wikipedia defines Hibernate as “…an object-relational mapping (ORM) library for the Java language, providing a framework for mapping an object-oriented domain model to a traditional relational database.


Although that’s quite a mouthful, at its simplest, Hibernate really is just a way to link an application written in Java to a relational database (or RDBMS like the popular MySQL).

If you have ever done database programming using a procedural language like PHP or ColdFusion, you’ll remember being able to open a connection to a datasource from within your code, executing SQL commands and formatting the returned data so as to present it on-screen to the user.  Or perhaps you would write form data to a database table using INSERT commands or even calling stored procedures that contained SQL code.

In a Java application you can do this too, however the overhead of maintaining complex and tedious SQL statements along with trails of JDBC connection strings can become overwhelming especially in any large-scale application.  Hibernate in this case offers a solution.

Basic Hibernate Architecture

Take a look at the diagram below.  You can plug Hibernate into your Java application as simply as adding a couple of jar files, a configuration script (in XML), and at least one persistent object mapping (for example, “person.hbm.xml” which maps the Person() object to the db.person table).  The configuration script (hibernate.cfg.xml) contains various settings and connection information (like username & password) to your database.


What Hibernate primarily allows you to do is map the domain model objects (usually POJOs)  you’ve already written as part of your enterprise Java application to the table and column structures in your database.  A Person() object and its properties can map directly to a PERSON table and its columns.  Once you’ve told Hibernate how to map Person() to db.PERSON (though special config files written in XML or annotations), performing simple CRUD (create, read, update and delete) operations is easy and done in an object-oriented way.




Reasons to choose Hibernate


Hibernate is free and open-source.


Since its creation in 2001 by Gavin King, Hibernate has gained enormous popularity within the Java community.  The reasons for this are largely in part due to excellent documentation, ease of use, features and of course, cost.  Because of this popularity, finding answers to puzzling problems is no farther away than a simple Google search.  This effectively lowers the overall cost of implementation since less hours will be consumed by developers implementing a Hibernate solution.

Demand for Hibernate developers has exploded compared to that of other ORM frameworks, as indicated by the graph below.  The odds of finding qualified programmers to work on your Hibernate project are invariably high, assuming of course they haven’t been snatched up by all of the other companies seeking suitable talent.



Hibernate conforms (mostly) to the JPA (Java Persistence Architecture) specification, which is the Java standard for accessing, persisting and managing data between Java objects and a relational database.  Oracle’s TopLink also conforms to JPA.  If you happen to be using something other than a relational database, JPA would not then be a solution for your persistence needs.


One reason to use an ORM solution like Hibernate is portability.  Because the underlying SQL database and dialect are abstracted away, switching to another database requires only a few changes to the Hibernate configuration files.  The objects and XML mappings can remain unchanged.


Hibernate (currently in its 4th iteration) comes packed with enough features to satisfy most needs.  Among the most talked about features of Hibernate:

  • Transparent Persistence

Any class can be used for persistence.  There is no (hidden) super or abstract classes that need to be extended in order to store data to a database from an object-oriented application.  Hibernate provides transparent persistence.  This is in direct contrast to an application that must use embedded SQL or JDBC calls to store or manipulate data.  Rather than utilize bytecode processing or code generation, Hibernate uses runtime reflection to determine the persistent properties of a class.  Hibernate generates the SQL calls and relieves the developer from manual result set handling and object conversion.

  • Query Support

Hibernate provides a SQL inspired language called Hibernate Query Language (HQL) which allows SQL-like queries to be written against Hibernate’s data objects.

  • Performance

Hibernate can be very fast, assuming you take advantage of some of its performance features, like:

Lazy-loading:  Hibernate will not load children of a parent object automatically.  It will only load them upon request unless explicitly told otherwise.  This is a design pattern of enterprise applications: only take what you need.  By not loading the entire object graph, you can ideally achieve better performance.

Caching:  Between first-level or L1 (caching within each Session), and second-level or L2   (caching shared between multiple Sessions), caching effectively minimizes the number of SQL queries and transactions that will hit a database, thereby increasing performance.  L1 first-level caching is enabled by default in Hibernate.

During my next post on Hibernate, we will walk through the creation of a simple Java project from scratch, configure our Hibernate mappings, create some POJO’s and a MySQL database.  Then we will run our application, add and retrieve data from our database and discuss other options and configurations that Hibernate is capable of.

About the Author: Erich Cervantez

Leave a comment