01 - Introduction to Hibernate

1.1 Introduction

All applications require some persistent  mechanism to store the data to avoid data loss. In Java applications generally relational databases are used to store the data. With traditional JDBC, developer has to write a bunch of boilerplate code and takes the responsibility of opening, closing of connections, statements, result set  etc. Also with traditional JDBC, the developer has to manually process the result set to construct an object.

With Object oriented programming languages like Java, C++ etc., everything is an object and so is the need to store the object representations in a database. To map the Object to relational databases there are ORM (Object Relational Mapping) frameworks available like iBatis, Spring DAO, Hibernate, etc. whose primary responsibility is the mapping of data between relational database and Objects.

All that the ORMs provide a set of API to interact with the database and perform operations on a database along with features like transactions, dirty checking etc.

1.2 Features and Advantages

Hibernate is one of the most powerful , widely used, open source and freely available product for the efficient persistence of Java objects. Hibernate provides a way to map our POJO  objects to relational database tables.

Using Hibernate as a persistence framework provides an opportunity for developers to concentrate on the business logic instead of putting efforts on SQLs, writing repetitive code which is required in traditional JDBC.

                                                          Fig - Hibernate

There are several advantages of hibernate like –

 

  • Provides support for over  30 dialects
  • Using Hibernate as a persistence framework makes application database independent.
  • Hibernate provides high performance
  • Hibernate supports caching and Lazy Loading.
  • Application code is more clean and easy to maintain. Developers need not to write SQLs , repetitive and unnecessary code.
  • Any change at a database level can be accommodated by changing the configuration file.
  • Only the changed properties of an object are updated in the database instead of updating complete row. This feature provides more optimization and better performance as compared to JDBC.
  • Hibernate provides a tool to auto generate schema from configuration files.
  • Hibernate provides support for concurrency and transaction management.
  • We can map multiple Java objects to one table.
  • Hibernate supports inheritance.
  • Hibernate can be used even with standalone applications which means we do not need any specific environment or container.
  • POJOs need not to implement any specific interfaces and all which means that
  • POJOs are reusable and existing POJOs can be persisted easily with the help of Hibernate.

 

1.3 Comparison with JPA

JPA stands for Java Persistence API and is a set of specification and not an implementation. All the JPA provides needs to implement and follow the specifications provided by JPA.

Hibernate is one of the JPA provider which means Hibernate implements the JPA specifications.

 Term “Hibernate with JPA” means using the JPA implementation by Hibernate which helps us in replacing Hibernate with other JPA implementation easily.

In simple words – JPA is an interface where as Hibernate is an implementation of JPA.

1.4 Why to use Hibernate

Any framework becomes popular and widely used because of its features. We have discussed several features  and advantages of hibernate in this chapter, but some of the key reasons of using hibernate are-

  1. Performance – Hibernate  offers a great performance several techniques like multiple level of caching, updates the changed fields of an object in table instead of updating entire row. Hibernate allows integration with several cache frameworks as well. 
  2. Database Independent code –  Applications using hibernate as persistence framework does not require any SQL statements in a code and hibernate manages the generation of database specific code based on the dialect used. This is a very important feature as most of the development environments use MySQL database where in production environments other relational databases like Oracle etc are being used. Only a change in dialect will be required.
  3. High Productivity –  With traditional JDBC, developers need to write a bunch of repetitive code like loading a driver, opening and closing the connections, statements, iterate result sets etc. . Hibernate does take care of these codes and allows developers to focus on business logic which results in high productivity.
  4. Less Maintainability –Most of the mappings and configurations  are done with the help of configuration files  which results in less code and hence less maintainability.

Like us on Facebook