Introduction to Hibernate

The objective of this article is to give introduction to hibernate.  Note that in here I not give at least single bit of code but still you can get the idea about the Hibernate if you are a novel one. You can read details under the following subtopics.
  • What is Hibernate?
  • Advantages of Hibernate
  • Disadvantages of Hibernate
  • Suitability of Using Hibernate
  • HQL
  •  Story behind the name ‘Hibernate’
What is Hibernate?
According to the Wikipedia “Hibernate is 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. Hibernate solves object-relational impedance mismatch problems by replacing direct persistence-related database accesses with high-level object handling functions.” In other words, Hibernate combine both features of relational database model and the object-oriented model to enhance the functionalities of the language or enhance the capabilities of the language. We call it as object-relational mapping or ORM.

By using Hibernate technology or Hibernate concept we can overcome the object-relational impedance mismatch problem. The object-relational impedance mismatch is a set of conceptual and technical difficulties that are often encountered when a relational database management system (RDBMS) is being used by a program written in an object-oriented programming language or style; particularly when objects or class definitions are mapped in a straightforward way to database tables or relational schemata. This practice has been recommended and documented by some object-oriented literature as a way to use databases in object-oriented programs.

Hibernate's main feature is mapping from Java classes to database tables. Moreover, Hibernate provides data query and retrieval facilities & generates the SQL calls and attempts to relieve the developer from manual result set handling and object conversion and keep the application portable to all supported SQL databases with little performance overhead.

Mapping Java classes to database tables is accomplished through the configuration of XML file or by using Java Annotations. When using an XML file, Hibernate can generate skeletal source code for the persistence classes. This is unnecessary when annotation is used. Hibernate can use the XML file or the annotation to maintain the database schema.

Facilities to arrange one-to-many and many-to-many relationships between classes are provided. In addition to managing association between objects, Hibernate can also manage reflexive associations where an object has a one-to-many relationship with other instances of its own type.

Hibernate supports the mapping of custom value types. This makes the following scenarios possible:
• Overriding the default SQL type that Hibernate chooses when mapping a column to a property.
• Mapping Java Enum to columns as if they were regular properties.
• Mapping a single property to multiple columns.

Hibernate provides transparent persistence for Plain Old Java Objects (POJOs). The name ‘POJO’ is used to emphasize that a given object is an ordinary Java Object, not a special object, and in particular not an Enterprise JavaBean.

Now I think you know what Hibernate is and some basic features of Hibernate. Let’s consider some advantages of Hibernate.
Advantages of Hibernate

There are some advantages in Hibernate. I’m going to list them below.

• Heterogeneity or database independent - Hibernate can work with different databases without difficulty. Same code will work for all databases like ORACLE,MySQL ,SQLServer or any other. We need not to change Persistency code depending on DB but JDBC query must be data base specific.

• Easy to learn – In Hibernate TABLE treat as an Object so no need to learn SQL language.

• No Query tuning - Don't need Query tuning in Hibernate. Hibernate automatically tuned query and return best result with performance If use Criteria Quires in Hibernate. But JDBC needs Query tuning.
In query tuning, the main focus is to execute the existing queries with the best performance possible. By creating indexes, retrieving only the necessary columns and rows with correct where clauses, using indexed views, using pre-calculated values, spreading tables over multiple disks, etc., a given query's speed can be increased tremendously. However, there is a limit to the extent this can be achieved. After this, extra resources like more memory or faster disks can be added.

• Supports automatic versioning- Hibernate Supports automatic versioning of rows. Automatic versioning is a simple technique for assuring data integrity.

• Cashe support- Hibernate support two level of cache. Cause to enhance the performance.

• Fast development – Developer don’t need to write queries.

• No need connection pool - A connection pool is a cache of database connections maintained so that the connections can be reused when future requests to the database are required. Connection pools are used to enhance the performance of executing commands on a database. Connection pooling also cuts down on the amount of time a user must wait to establish a connection to the database. But using Hibernate user can gain the advantage of connection pool automatically.

• Easier to understand the system – Because of the XML file all the relations between tables can be easily identify.

• Can load objects on start up

• Easy to customize

In deeply, but as a fresher you can jump over below bullets;

• Enhance productivity - Improves the productivity by eliminating the JDBC tedious code and reducing No. of lines.

• Efficient Collection Handling - Hibernate only ever inserts/updates/deletes collection rows that actually changed.

• Caching objects- The session is a transaction-level cache of persistent objects. User may also enable a JVM-level/cluster cache to memory and/or local disk.

• Executing SQL statements later- The session never issues an INSERT or UPDATE until it is actually needed. So if an exception occurs and you need to abort the transaction, some statements will never actually be issued. Furthermore, this keeps lock times in the database as short as possible (from the late UPDATE to the transaction end).

• Never updating unmodified objects-It is very common in hand-coded JDBC to see the persistent state of an object updated, just in case it changed…..for example, the user pressed the save button but may not have edited any fields. Hibernate always knows if an object’s state actually changed, as long as user inside the same (possibly very long) unit of work.

• Rolling two updates into one-Hibernate can roll two seemingly unrelated updates of the same object into one UPDATE statement.

• Updating only the modified columns- Hibernate knows exactly which columns need updating and, if user chooses, will update only those columns.

• Outer join fetching- Hibernate implements a very efficient outer-join fetching algorithm! In addition, user can use subselect and batch pre-fetch optimizations.

• Lazy collection initialization.

• Lazy object initialization- Hibernate can use runtime-generated proxies (CGLIB) or interception injected through bytecode instrumentation at build-time.

Disadvantages of Hibernate

Any system or technology or concept can have disadvantages. Now I mention what are those in Hibernate.

• Debugging- Sometimes debugging and performance tuning becomes difficult.

• Slower than JDBC- Hibernate is slower than pure JDBC as it is generating lots of SQL statements in runtime.

• Not suitable for Batch processing- It advisable to use pure JDBC for batch processing.

• Overhead-Use of Hibernate is an overhead for the applications which are : simple and use one database that never change, need to put data to database tables, no further SQL queries and  there are no objects which are mapped to two different tables. Hibernate increases extra layers and complexity. So for these types of applications JDBC is the best choice.

• Difficult to maintain-Anybody wanting to maintain application using Hibernate will need to know Hibernate.

• Performance decreases in complex data-For complex data, mapping from Object-to-tables and vice versa reduces performance and increases time of conversion.

• Not allow all queries- Hibernate does not allow some type of queries which are supported by JDBC. For example it does not allow inserting multiple objects to same table using single query. Developer has to write separate query to insert each object.

• High memory consumption- Loading a large number of objects from the database which then reside in memory cause to waste the memory. One approach to resolving this is by using lazy loading, which has a few of its own caveats.

• Difficulties in legacy databases- Hibernate works best and easiest with a brand new database, attempting to use it on an existing legacy database can be quite a difficult task.

• Learning difficulties – In here you may mess with advantages. Note that  it is quite easy to do the basics in hibernate but to really use hibernate well you need a good knowledge of ORM concepts, the way mappings and the session works and also more advanced concepts such as caching, lazy loading and fetching strategies.

• Performance optimization -Trying to optimize performance at the database level is a bit trickier with Hibernate due to the fact the SQL that is executed is actually generated by Hibernate, hence user  not able to just go in and adjust the SQL accordingly.

Now Disadvantages also completed.

Suitability of Using Hibernate
“Why should choose Hibernate rather than other object-relational mapping techniques? “Is the problem which is going to address in this section. If you surf on cyber you can find several other object-relational mapping techniques such as EJB3 , Oracle Top Links , Cayenne , Open JPA , IBATIS,JPOX . In here you will understand the importance of Hibernate.

Productivity, Maintainability, Portability- As mentioned under advantages Hibernate provides all above Object-Relational benefits. Because Hibernate is combination of both object oriented model and relational database model.

Free- Hibernate is free and open source that is distributed under the GNU Lesser General Public License.
Easy to learn – Recall from advantages Hibernate is totally object orientated concept.

Code generation tool - Hibernate tools provided by community helps developer generate or develop hibernate application very fast and easy. (Eclipse’s Plugin & Code generation tools)

So those reasons impact to use Hibernate in IT industry today.


Hibernate Query Language or HQL for short is extremely powerful query language. HQL is much like SQL and are case-insensitive, except for the names of the Java Classes and properties. Hibernate Query Language is used to execute queries against database. Hibernate automatically generates the sql query and execute it against underlying database if HQL is used in the application. HQL is based on the relational object models and makes the SQL object oriented. Hibernate Query Language uses Classes and properties instead of tables and columns. Hibernate Query Language is extremely powerful and it supports Polymorphism, Associations, Much less verbose than SQL.

There are other options that can be used while using Hibernate. These are Query By Criteria (QBC) and Query BY Example (QBE) using Criteria API and the Native SQL queries.

Story behind the name 

The story behind the name of Hibernate (Actually it is not a story it is a fact.) In Hibernate “An object is sent to hibernation to a RDBMS, when it comes back (if it does) it wakes up from his hibernation.

Now, as a novel you may know the basics about Hibernate. Nowadays Hibernate is very popular we can just Google to read about Hibernate and In addition, there are many books, communities and forums on Hibernate. Moreover, Java Market need Hibernate developers, demand of Hibernate developer is in decent growth and compare to the other tools. Hibernate working experience definitely add advantages for your future career. That is advantage of learning Hibernate. Welcome to Hibernate!

You can leave a comment :)


  1. That what i was in need of Hibernate advantages and disadvantages as compare to JDBC.

  2. @ aman : Thanks for your comment.

  3. Anonymous20/7/11 23:34

    What happens when hibernate query returns collection

  4. This comment has been removed by the author.

  5. Anonymous21/7/11 10:36

    Nice introduction, easy to understand. Keep it up..

  6. Anonymous21/9/11 16:14

    Nice article to bigin with Hibernate.

  7. Thanks for all of you.........

    1. hi Hareendra Perera

      Great tutorial for hibernate,what is hibernate and advantages and disadvantages and lot about hibernate.
      Can u please post or direct me for coding with sql in hibernate instead if HQL please help me on this.

  8. Anonymous3/10/11 15:19

    Hi Perera,
    I am new to hibernate. ur article was nice. few points i can't understand. Could u please explain on more..

    "Not allow all queries- Hibernate does not allow some type of queries which are supported by JDBC. For example it does not allow inserting multiple objects to same table using single query. Developer has to write separate query to insert each object. " - Could u please give one example for me? Thanks in advance.

    1. As in oracle,there is a concept of scripting,where we write & with the column name ex...insert into emp values(&empid,&empname)..this can be used to enter multiple row values in single shot.But this type of feature is not provided in hibernate..Hope i have answered your question.

  9. Anonymous1/8/12 13:55

    Really a worh reading material. Thanks

  10. hi Hareendra Perera

    Great tutorial for hibernate,what is hibernate and advantages and disadvantages and lot about hibernate.
    Can u please post or direct me for coding with sql in hibernate instead if HQL please help me on this.