Everything Java

← Back to blog

Published on Thu Aug 14 2025 15:15:00 GMT+0000 (Coordinated Universal Time) by Purusothaman Ramanujam

Introduction to Hibernate ORM: Mapping Java Objects to Database Tables

Introduction

Most Java applications need to store and retrieve data from a database. Writing SQL and managing database connections manually can be tedious and error-prone. Hibernate ORM (Object-Relational Mapping) is a powerful framework that automates the mapping between Java objects and database tables, making database operations much easier.

In this post, you’ll learn the basics of Hibernate and how to map Java classes to database tables.

What is Hibernate?

Hibernate is an open-source ORM framework for Java. It:

Adding Hibernate to Your Project

Maven Dependencies

<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.3.1.Final</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.9</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.2.224</version>
<scope>runtime</scope>
</dependency>

Gradle Dependencies

implementation 'org.hibernate.orm:hibernate-core:6.3.1.Final'
implementation 'org.slf4j:slf4j-api:2.0.9'
runtimeOnly 'com.h2database:h2:2.2.224'

Mapping a Java Class to a Table

Let’s create a simple User entity and map it to a database table.

import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String email;
// Getters and setters
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getEmail() { return email; }
public void setEmail(String email) { this.email = email; }
}

Hibernate Configuration

Create a hibernate.cfg.xml file in src/main/resources:

<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
<property name="hibernate.connection.driver_class">org.h2.Driver</property>
<property name="hibernate.connection.url">jdbc:h2:mem:testdb</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<mapping class="User"/>
</session-factory>
</hibernate-configuration>

Basic CRUD Operations

Here’s how to save and retrieve a user:

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateExample {
public static void main(String[] args) {
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
// Create and save a user
User user = new User();
user.setName("Alice");
user.setEmail("alice@example.com");
session.save(user);
// Retrieve user by ID
User retrieved = session.get(User.class, user.getId());
System.out.println("User: " + retrieved.getName() + ", " + retrieved.getEmail());
session.getTransaction().commit();
session.close();
sessionFactory.close();
}
}

Best Practices

  1. Use Entities for Data: Map only your data classes as entities.
  2. Close Sessions: Always close sessions and session factories.
  3. Use Transactions: Wrap database operations in transactions.
  4. Avoid N+1 Problem: Use fetch strategies wisely.
  5. Validate Entities: Use validation annotations for data integrity.

Conclusion

Hibernate ORM makes it easy to map Java objects to database tables and handle database operations. Start with simple mappings, then explore relationships, queries, and advanced features as you grow more comfortable.

Resources

Written by Purusothaman Ramanujam

← Back to blog