Hibernate Tutorial

Last Updated : 29 May 2026
hibernate tutorial with example

This hibernate tutorial provides in-depth concepts of Hibernate Framework with simplified examples. It was started in 2001 by Gavin King as an alternative to EJB2 style entity bean.

Hibernate Framework

Hibernate is a Java framework that simplifies the development of Java application to interact with the database. It is an open source, lightweight, ORM (Object Relational Mapping) tool. Hibernate implements the specifications of JPA (Java Persistence API) for data persistence.

ORM Tool

An ORM tool simplifies the data creation, data manipulation and data access. It is a programming technique that maps the object to the data stored in the database.

hibernate tutorial, An introduction to hibernate

The ORM tool internally uses the JDBC API to interact with the database.

What is JPA?

Java Persistence API (JPA) is a Java specification that provides certain functionality and standard to ORM tools. The javax.persistence package contains the JPA classes and interfaces.

Advantages of Hibernate Framework

Following are the advantages of hibernate framework:

1) Open Source and Lightweight

Hibernate framework is open source under the LGPL license and lightweight.

2) Fast Performance

The performance of hibernate framework is fast because cache is internally used in hibernate framework. There are two types of cache in hibernate framework first level cache and second level cache. First level cache is enabled by default.

3) Database Independent Query

HQL (Hibernate Query Language) is the object-oriented version of SQL. It generates the database independent queries. So you don't need to write database specific queries. Before Hibernate, if database is changed for the project, we need to change the SQL query as well that leads to the maintenance problem.

4) Automatic Table Creation

Hibernate framework provides the facility to create the tables of the database automatically. So there is no need to create tables in the database manually.

5) Simplifies Complex Join

Fetching data from multiple tables is easy in hibernate framework.

6) Provides Query Statistics and Database Status

Hibernate supports Query cache and provide statistics about query and database status.

Hibernate Index

Prerequisites to Learn Hibernate

Before learning Hibernate, you should have a basic understanding of Java programming, Object-Oriented Programming (OOP) concepts, JDBC, relational databases, and SQL queries. Familiarity with Java development tools and web application development will also help you learn Hibernate more effectively.

Audience to Learn Hibernate

This Hibernate tutorial is designed for students, beginners, software developers, Java programmers, and professionals who want to simplify database operations in Java applications. It is suitable for anyone interested in learning Object Relational Mapping (ORM) and enterprise application development.

Career Opportunities after Learning Hibernate

After learning Hibernate, you can pursue roles such as Java Developer, Backend Developer, Full Stack Developer, Software Engineer, Enterprise Application Developer, and Spring Boot Developer. Hibernate is widely used in enterprise applications that makes it a valuable skill for building database-driven Java applications.

Hibernate MCQs

1. Which of the following statements about Hibernate caching is true?

  1. Hibernate does not support caching mechanisms.
  2. Hibernate supports only first-level cache.
  3. Hibernate supports both first-level and second-level caching mechanisms.
  4. Hibernate supports caching only for read-only transactions.
 

Answer: C

Explanation: Hibernate supports two levels of caching: the first-level cache is associated with the session object and is enabled by default, while the second-level cache is associated with the session factory and can be configured to improve performance by reducing database access.


2. In Hibernate, which annotation is used to map a Java class to a database table?

  1. @Entity
  2. @Table
  3. @Column
  4. @JoinColumn
 

Answer: A

Explanation:s The @Entity annotation is used in Hibernate to specify that a particular class is an entity and should be mapped to a database table. The @Table annotation can also be used to provide additional details about the table mapping, but it is not required for basic mapping.

3. Which of the following is not a valid Hibernate inheritance strategy?

  1. SINGLE_TABLE
  2. TABLE_PER_CLASS
  3. JOINED
  4. MIXED_TABLE
 

Answer: D

Explanation: Hibernate supports three main inheritance strategies: SINGLE_TABLE (single table per hierarchy), TABLE_PER_CLASS (table per concrete class), and JOINED (table per class with relationships). MIXED_TABLE is not a recognized inheritance strategy in Hibernate.

4. What is the purpose of the Hibernate SessionFactory?

  1. To manage the configuration and lifecycle of database connections.
  2. To store the persistent state of objects.
  3. To serve as a factory for Session objects.
  4. To map Java objects to database tables.
 

Answer: C

Explanation: The SessionFactory in Hibernate is responsible for creating Session objects. It is a heavyweight object that is created once during application initialization and provides a factory method for Session instances, which are lightweight and short-lived.

5. When using HQL (Hibernate Query Language), which of the following is true regarding named parameters?

  1. Named parameters must start with a question mark (?).
  2. Named parameters are case-insensitive.
  3. Named parameters must start with a colon (:).
  4. Named parameters cannot be used in HQL.
 

Answer: C

Explanation: In HQL, named parameters are identified by a leading colon (:) followed by the parameter name. For example, :paramName is a valid named parameter in HQL. It allows for more readable and maintainable queries compared to positional parameters.