Hibernate : An Introduction

Hibernate is a Java framework which simplifies the development of Java application to where interaction with database is required (basically every other modern application). It is an open source, lightweight, ORM (Object Relational Mapping) tool. Hibernate implements the specification of the JPA (Java Persistent API) for data persistence.

What is an ORM tool ?

Consider the classic case of persisting (storing) something on to a database table. The usual flow is creating the table and updating the same by JDBC calls and other similar methods depending on the language you code in. Somehow, all of this doesn’t make sense intuitively since we are storing something equivalent of a data structure in the database table. How good it would be if we can make the entity to be persisted from the Java side itself and let that framework handle all the nitty gritty details of creating connection and persisting things etc ? Well, we are lucky in that case since we have something called as an ORM (Object Relation Mapping) tool for doing these things for us. An ORM tool simplifies the creation of data for us, manipulating the data and accessing it. It is a programming technique by virtue of which the ORM frameworks map the object to the data stored in the database.

ORM tool internally uses the JDBC APIs to remove the boiler plate coding whereas the programmer may focus on the core business logic.

What Is JPA ?

Java Persistence API or JPA in short is a Java specification which provides certain functionality and standard ORM tools. The javax.persistence package contains the JPA classes and interfaces.

Advantages of Using the Hibernate Framework

  1. Open Source & Lightweight
    Hibernate framework is open source under the LGPL licence and quite lightweight.
  2. Fast Performance
    Hibernate is relatively faster than the actual JDBC approach both in the context of actual transactional speed and implementational speed. Hibernate uses two types of caches internally where first level cache is enabled by default and the second level cache may be enabled when required.
  3. Database Independent Query
    HQL (Hibernate Query Language) is the object oriented version of SQL. It generates queries which aren’t specific to any particular database, in a sense they are loosely coupled. Incases where we don’t use any sort of hibernate or any other ORM tool for that matter when the database changes the query changes as well. But in the cases where we are using any ORM we just change the dialect name for the database and everything works fine.
  4. Automatic Table Creation
    Hibernate can easily create tables for us in the database from Java code side, thus we don’t need to create tables manually in the database. This feature can be leveraged sparingly as and where required.


Hibernate Architecture

Hibernate has a layered architecture and it helps the users to interact and leverage the framework without having to know the internal APIs and details. The database and configuration data are used to provide persistence services and persistent objects to the application.

Hibernate Architecture

Let’s briefly understand what each block means

  1. Configuration
    This information is provided to the framework either as part of the file hibernate.cfg.xml file. For Java based configuration you may annotate the class with @Configuration annotation. This information is used by the Session Factory to work with Java Application and the databases.
  2. Session Factory
    Whenever there is an interaction required with the database, a session is needed. The work of Session Factory is to create the Session objects. Session Factory uses the configuration information from the above listed files to instantiate the session object accordingly and appropriately.
  3. Session
    This represents the interaction between the application and the databases. org.hibernate.Session class represents this entity. The instance of a session can be retrieved from the SessionFactory bean.
  4. Query
    Query is used to query the database for persisted objects. NamedQuery and Criteria API are used to query the database.
  5. First Level Cache
    It is the default cache which is used by the Hibernate Session object while interacting with the database. It is also known as session cache and it caches object within the current session. All requests from the Session object to database pass through the first level cache or session cache. This level of caching is available only with the session object until the session object is alive.
  6. Transaction
    Transaction helps in data consistency and rollback incases when something goes wrong or unexpected.
  7. Persistent Objects
    These are the POJOs which get persisted as one of the rows in the related table in the database by hibernate. The objects to be persisted are marked as @Entity
  8. Second Level Cache
    This level of caching is used to store objects across sessions. This needs to be explicitly enabled and requires a cache provider for the second level caching. EhCache is one such cache provider for the second level caching.

In the upcoming posts we shall deep dive into the concepts of hibernate and create some mini projects to cement our learning 🙂 Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *

Design A Cloud Storage System Like Drop Box

How To Get Started With Lambda Expressions In Java