Hibernate Framework Overview - Architecture and Basics


Before getting started with the Hibernate framework, let's familiarize ourselves with a few basic concepts of the hibernate framework, its architecture, its 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 a java based ORM tool that provides a framework for mapping application domain objects to the relational database tables and vice versa.

Hibernate is probably the most popular JPA implementation and one of the most popular Java frameworks in general. Hibernate acts as an additional layer on top of JDBC and enables you to implement a database-independent persistence layer. It provides an object-relational mapping implementation that maps your database records to Java objects and generates the required SQL statements to replicate all operations to the database.
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.

Hibernate acts as an additional layer on top of JDBC and enables you to implement a database-independent persistence layer:

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 is 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 the 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, which 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 is 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 performs 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 the 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 which is a checked exception, so we need to write a lot of try-catch block code. Most of the time it’s redundant in every JDBC call and used for transaction management. Hibernate wraps JDBC exceptions and throws 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 applications and completely missing in JDBC API.
  • Hibernate supports JPA annotations, so the code is independent of the implementation and easily replaceable with other ORM tools. JDBC code is very tightly coupled with the application.

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours


Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course

Comments