Hibernate Framework Overview - Architecture and Basics

Before getting started with Hibernate framework, let's familiar with a few basic concepts of the hibernate framework, it's architecture, it's benefits and advantages over JDBC etc.
Let's start with what is ORM?

What is ORM?

Object-relational mapping or ORM is the programming technique to map application domain model objects to the relational database tables. Hibernate is a Java-based ORM tool that provides a framework for mapping application domain objects to the relational database tables and vice versa.

Read more about ORM and why to use ORM at https://www.tutorialspoint.com/hibernate/orm_overview.htm.

What is Hibernate Framework?

Hibernate is an Object/Relational Mapping solution for Java environments. Object-relational mapping or ORM is the programming technique to map application domain model objects to the relational database tables. Hibernate is a java based ORM tool that provides a framework for mapping application domain objects to the relational database tables and vice versa.
Hibernate provides an implementation or provider of the Java Persistence API, that makes it a great choice as an ORM tool with benefits of loose coupling. We can use the Hibernate persistence API for CRUD operations. Hibernate framework provides an option to map plain old java objects to traditional database tables with the use of JPA annotations as well as XML based configuration.

Official Hibernate website at http://hibernate.org/orm.

Example: Below diagram shows an Object Relational Mapping between Student Java class and student table in the database.


What is the Java Persistence API (JPA)?

Java Persistence API (JPA) provides a specification for managing the relational data in applications.
JPA specifications are defined with annotations in a javax.persistence package. Using JPA annotation helps us in writing implementation independent code.

Learn complete JPA at JPA Tutorial - Java Persistence API

How does Hibernate relate to JDBC?

Hibernate uses JDBC for all database communications. Hibernate uses JDBC to interact with the database.

System Requirements

  • Hibernate 5.2 and later versions require at least Java 1.8 and JDBC 4.2.
  • Hibernate 5.1 and older versions require at least Java 1.6 and JDBC 4.0.

Architecture

Hibernate, as an ORM solution, effectively "sits between" the Java application data access layer and the Relational Database, as can be seen in the diagram above. The Java application makes use of the Hibernate APIs to load, store, query, etc its domain data. Here we will introduce the essential Hibernate APIs.
As a JPA provider, Hibernate implements the Java Persistence API specifications and the association between JPA interfaces and Hibernate specific implementations can be visualized in the following diagram:

SessionFactory (org.hibernate.SessionFactory)

A thread-safe (and immutable) representation of the mapping of the application domain model to a database. Acts as a factory for org.hibernate.Session instances. The EntityManagerFactory is the JPA equivalent of a SessionFactory and basically, those two converge into the same SessionFactory implementation.
A SessionFactory is very expensive to create, so, for any given database, the application should have only one associated SessionFactory. The SessionFactory maintains services that Hibernate uses across all Session(s) such as second level caches, connection pools, transaction system integrations, etc.

Session (org.hibernate.Session)

A single-threaded, short-lived object conceptually modeling a "Unit of Work". In JPA nomenclature, the Session is represented by an EntityManager.
Behind the scenes, the Hibernate Session wraps a JDBC java.sql.Connection and acts as a factory for org.hibernate.Transaction instances. It maintains a generally "repeatable read" persistence context (first level cache) of the application domain model.

Transaction (org.hibernate.Transaction)

A single-threaded, short-lived object used by the application to demarcate individual physical transaction boundaries. EntityTransaction is the JPA equivalent and both acts as an abstraction API to isolate the application from the underlying transaction system in use (JDBC or JTA).

Know more about JPA architecture at https://www.javaguides.net/2018/12/jpa-architecture.html.

What are the important benefits of using Hibernate Framework?

Some of the important benefits of using hibernate framework are:
  • Hibernate eliminates all the boiler-plate code that comes with JDBC and takes care of managing resources, so we can focus on business logic.
  • Hibernate framework provides support for XML as well as JPA annotations, that makes our code implementation independent.
  • Hibernate provides a powerful query language (HQL) that is similar to SQL. However, HQL is fully object-oriented and understands concepts like inheritance, pol, morphism, and association.
  • Hibernate is an open source project from Red Hat Community and used worldwide. This makes it a better choice than others because the learning curve is small and there are tons of online documentation and help is easily available in forums.
  • Hibernate is easy to integrate with other Java EE frameworks, it’s so popular that Spring Framework provides built-in support for integrating hibernate with Spring applications.
  • Hibernate supports lazy initialization using proxy objects and perform actual database queries only when it’s required.
  • Hibernate cache helps us in getting better performance.
  • For a database vendor specific feature, hibernate is suitable because we can also execute native SQL queries.
  • Overall hibernate is the best choice in the current market for ORM tool, it contains all the features that you will ever need in an ORM tool.

What are the advantages of Hibernate over JDBC?

Some of the important advantages of Hibernate framework over JDBC are:
  • Hibernate removes a lot of boiler-plate code that comes with JDBC API, the code looks cleaner and readable.
  • Hibernate supports inheritance, associations, and collections. These features are not present with JDBC API.
  • Hibernate implicitly provides transaction management, in fact, most of the queries can’t be executed outside a transaction. In JDBC API, we need to write code for transaction management using commit and rollback. 
  • JDBC API throws SQLException that is a checked exception, so we need to write a lot of try-catch block code. Most of the times it’s redundant in every JDBC call and used for transaction management. Hibernate wraps JDBC exceptions and throw JDBCException or HibernateException un-checked exception, so we don’t need to write code to handle it. Hibernate built-in transaction management removes the usage of try-catch blocks.
  • Hibernate Query Language (HQL) is more object-oriented and close to a Java programming language. For JDBC, we need to write native SQL queries.
  • Hibernate supports caching that is better for performance, JDBC queries are not cached hence performance is low.
  • Hibernate provides an option through which we can create database tables too, for JDBC tables must exist in the database.
  • Hibernate configuration helps us in using JDBC like connection as well as JNDI DataSource for a connection pool. This is a very important feature in enterprise application and completely missing in JDBC API.
  • Hibernate supports JPA annotations, so the code is independent of implementation and easily replaceable with other ORM tools. JDBC code is very tightly coupled with the application.

Comments