What are the advantages of Hibernate?

In this post, we will evaluate advantages of Hibernate and explain how Hibernate provides database independence.

When creating a product, we only have limited visibility into clients’ demands. In service industry (companies like Infosys, TCS, and others), any code must be strictly compliant with requirements of the client who ordered it.

The product industry’s requirements are largely a result of marketing team’s research about what kind of applications are available in the market and what new can be added to them. This does not prepare them for the unpredictability that comes when trying to sell the product.

You design your product to work with the latest technologies – who wants to work with a decade-old technology when faster and more efficient alternatives are available, you think! But rarely (if ever!) you will encounter clients who want the stock version of your app; most will look for customization to continue working with their legacy systems.

With databases, this situation gets even more complex, because there are literally hundreds of different types of databases are available in the market. You can’t keep changing your code for every time a new customization request comes in. The industry is very sensitive about turn-around times and the competitors are always a step of us.

This is where Hibernate comes in! Unlike JDBC (which is a database connection technique), Hibernate is an ORM framework. ORM stands for Object Relational Mapping. Hibernate runs in between the JDBC and business logic layer).

JDBC connects you to a specific kind of database. If you change the database, your JDBC connection parameters also change. Hibernate is said to be database agnostic i.e. it allows you to code for multiple databases by providing a generic mapping between a Java class and its corresponding table in the database; the database layer at the bottom could be any kind of database. Since Hibernate is generic, your code won’t need to be customized for every different source database.

How does Hibernate achieve database independence?

Hibernate is an ORM (Object Relational Mapping) Framework. It provides API for storing Java objects into relational databases. It maps your classes to tables, objects to rows of tables, and class members as table columns. The developer has to specify these mappings inside a XML configuration file called hibernate.cfg.xml.

For persisting objects into a relational database, a traditional option is JDBC. However, JDBC runs into problems when the code has to be customized for different source databases.

Instead, Hibernate adds the notion of a dialect. Dialect is a database-specific class which converts standard SQL queries into those suitable for a vendor-specific database. We will see in further tutorials how to use a dialect. For now, it is sufficient to understand that dialect is a similar to your driver in JDBC, but it is not exactly a driver.

No queries is one of biggest advantages of Hibernate

Hibernate maps Java Objects to Tables in Relational Database

In real world, most languages have various versions and flavors according to the region they are spoken in. Some words may be added or removed, and some words may be spoken differently. Similarly, a dialect in Hibernate corresponds to a variation of SQL which the base database will accept. Dialect conforms to a database; as database changes, the corresponding dialect class used by Hibernate must also be changed.

What are some other advantages of Hibernate?

I think the beauty of Hibernate lies in its transparent execution of an operation. The programmer doesn’t need to write any SQL to execute a basic operation; Hibernate builds up a query for you, and even gives you an option to show the generated queries on your console.

Some other advantages of Hibernate are as follows:

  1. Use of prepared queries: By using prepared queries, Hibernate compiles your query only once and simply replaces the parameters on repeated executions. This reduces the time required to generate a query and improves the overall turnaround time for the operations. JDBC on the other hand lets the developer freely choose the type of query to execute. A static statement compiles every time before execution, even if there is no change in the query.
  2. Concept of caching: To avoid frequent hits to a database to fetch large data sets, Hibernate adds a first level cache as well as a second level cache. A third query level cache also exists for repeatedly executing queries. This helps improve the performance of the whole application many folds.
  3. Automated DDL Queries: If you try to persist an object whose class doesn’t have a corresponding table in the database, Hibernate will first create the table for you and then insert the record. Being an ORM tool, Hibernate converts the class definition into a table schema definition.
  4. Support for sequence algorithms for primary key generation: A database uses sequence algorithms to automatically generate primary keys. Hibernate lets you configure your own sequence algorithm or use the one provided by the database. There is no such facility in JDBC.
  5. Support for SQL-like query language: Hibernate also provides Hibernate Query Language (HQL) which is similar to SQL but works with objects and their properties rather than tables and columns. The Hibernate Session object permits only limited operations, HQL expands the envelope of operations and lets you run precise SQL-like queries. The biggest advantage of Hibernate Query language is that it isn’t much different from SQL, so if you know SQL, you will find it easy to pick up HQL as well.
  6. Support for transactions and connection pooling: A connection pool is a collectively used bundle of collections.  In JDBC, all operations use raw, low-level classes. So connection pooling in JDBC involves manual configuration. Hibernate provides automated internal connection pooling, so that programmer doesn’t need to do it manually. Higher level processing with transactions is also an important feature that sets Hibernate apart form JDBC.

Of course, every technology comes with fair share of disadvantages. With Hibernate, you will not able to insert more than one object at a time. Using XML files also makes it complex.

Hibernate provides such an amazing usability boost that it is hard to ignore it for long, and many companies are now building their products with Hibernate integration so that it becomes easy to port the application for multiple databases.

Leave a Reply