Hibernate for Java: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction to Hibernate: What It Is and Why You Need It



Hibernate is a powerful, high-performance object-relational mapping (ORM) library for the Java language. It solves the fundamental problem known as "object-relational impedance mismatch": in Java we work with objects and classes, while in relational databases we work with tables and records. Hibernate automatically converts Java objects into SQL queries and back, allowing the developer to focus on business logic rather than writing tons of repetitive JDBC code.



Key features of Hibernate:

  • CRUD Automation: Creating, reading, updating, and deleting database records are performed through working with Java objects.
  • HQL (Hibernate Query Language): An object-oriented analog of SQL that works with class names and their fields, rather than tables and columns.
  • Cache Management: Built-in first-level and second-level cache mechanisms for improved performance.
  • Lazy Loading: Loading related data only at the moment it is actually used.
  • Database Schema Generation: The ability to automatically create or update database tables based on annotations in Java classes.
  • Transaction Support: Full integration with Java Transaction API (JTA) and local JDBC transactions.


Hibernate is the de facto standard for working with relational databases in the Java ecosystem. It is used in a huge number of projects — from small web applications to enterprise-level systems.



Installing Hibernate



Unlike Python or JavaScript, Java does not have a single package manager like pip or npm. Hibernate is added to a project through a build system such as Maven or Gradle. Below are examples for both tools.



Installation via Maven (recommended)

Add the following dependencies to your project's pom.xml file. We will use Hibernate 6.x (the current version at the time of writing) and the PostgreSQL driver (you can replace it with MySQL, H2, or another).



<dependencies>    <!-- Hibernate Core -->    <dependency>        <groupId>org.hibernate.orm</groupId>        <artifactId>hibernate-core</artifactId>        <version>6.3.1.Final</version>    </dependency>

<!-- PostgreSQL Driver --> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>42.7.1</version> </dependency>

<!-- Logging (optional, but useful for debugging) --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>2.0.9</version> </dependency></dependencies>


Installation via Gradle

Add to the build.gradle file:



dependencies {    implementation 'org.hibernate.orm:hibernate-core:6.3.1.Final'    implementation 'org.postgresql:postgresql:42.7.1'    implementation 'org.slf4j:slf4j-simple:2.0.9'}


After adding the dependencies, your IDE (IntelliJ IDEA, Eclipse) will automatically download all the necessary JAR files.



Quick Start with a Code Example



Let's create a simple application that saves and reads a User object from the database. We will use the H2 database (embedded) for simplicity, so we don't need to set up an external server.



Step 1: Configuration file hibernate.cfg.xml

Create it in the src/main/resources/

Recommendations