Java Persistence
Hibernate Tutorial
Learn Hibernate ORM from the fundamentals with clear explanations and practical examples.
Hibernate Framework
Hibernate is an open-source, lightweight Java ORM framework that simplifies application development when working with relational databases. It maps Java objects to database tables and manages much of the persistence work for you.
Hibernate originated in 2001, led by Gavin King, as an alternative to EJB2-style entity beans. Today, Hibernate is one of the most widely used implementations of the Jakarta Persistence specification.
What Is an ORM Tool?
Object-Relational Mapping (ORM) is a programming technique that maps objects in an application to rows and tables in a relational database.
| Java application | Relational database |
|---|---|
| Class | Table |
| Object | Row |
| Field | Column |
| Relationship | Foreign-key relationship |
Hibernate uses JDBC internally to communicate with the database, while providing entity mapping, query support, transactions, dirty checking, and caching at a higher level.
What Is JPA?
JPA, now called Jakarta Persistence, is a specification that defines a standard API and annotations for persistence in Java. Hibernate is a popular implementation of that specification; JPA is the contract, while Hibernate is the provider that performs the work.
javax.persistence. Modern Jakarta Persistence applications use jakarta.persistence. The correct package depends on the application and framework version.import jakarta.persistence.Entity;
import jakarta.persistence.Id;
@Entity
public class Employee {
@Id
private Long id;
private String name;
}Advantages of Hibernate
Open Source and Lightweight
Hibernate is open source and can be added as a library to Java applications. It reduces repetitive JDBC mapping code while leaving application code focused on domain behavior.
Fast Performance with Caching
Hibernate supports a first-level cache associated with the persistence context and optional second-level caching. The first-level cache is enabled by default. Caching should still be configured and measured carefully for the workload.
Database-Independent Queries
HQL and JPQL query entities and their properties rather than database-specific table details. Hibernate translates them to SQL for the configured database, which can reduce database portability work.
String hql = "from Employee e where e.department = :department";
List<Employee> employees = session
.createQuery(hql, Employee.class)
.setParameter("department", "Engineering")
.getResultList();Schema Generation Support
Hibernate can create or update database schemas in development through configuration. Production systems should usually use a versioned migration tool such as Flyway or Liquibase instead of relying on automatic schema updates.
Simplifies Complex Joins
Entity relationships allow applications to navigate related data through objects. Fetch joins and carefully designed queries can load related data efficiently, while avoiding unnecessary eager loading.
Query Statistics and Database Visibility
Hibernate can expose statistics such as query counts, entity operations, cache activity, and session behavior. These metrics help diagnose slow queries, N+1 query problems, and inefficient persistence workflows.
How Hibernate Fits in an Application
Applications typically define entities, obtain a persistence context, perform work inside a transaction, and let Hibernate flush changes as SQL. The persistence context tracks managed objects and performs dirty checking before flush or commit.
