Skip to main content
twynedocs.com

Back to all posts

How to Set Up Hibernate with a Mysql Database in 2026?

Published on
3 min read
How to Set Up Hibernate with a Mysql Database in 2026? image

Best Hibernate Setup Tools to Buy for MySQL Databases in 2026 in January 2026

+
ONE MORE?

In 2025, integrating Hibernate with a MySQL database remains an essential skill for Java developers. Hibernate continues to offer a robust framework for object-relational mapping (ORM) in Java applications. This guide will walk you through the steps to set up Hibernate with a MySQL database efficiently.

Prerequisites

Before setting up Hibernate, ensure you have the following:

  1. Java Development Kit (JDK): Make sure the latest JDK is installed on your machine.
  2. MySQL Database: Install MySQL Server and create a database for your project.
  3. Maven or Gradle: To manage your project’s dependencies, ensure you have Maven or Gradle installed.

Step-by-Step Guide

Step 1: Add Hibernate and MySQL Dependencies

First, include Hibernate and MySQL in your project’s dependencies. If you’re using Maven, add the following to your pom.xml:

<!-- MySQL Connector -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.33</version> <!-- Ensure this is the latest version -->
</dependency>

<!-- Other dependencies -->
<!-- e.g., Logging, JPA, etc. -->

For Gradle users, add the following to your build.gradle:

dependencies { implementation 'org.hibernate:hibernate-core:5.6.15.Final' implementation 'mysql:mysql-connector-java:8.0.33' }

Step 2: Configure Hibernate

Create a hibernate.cfg.xml file in your src/main/resources directory with the following content:

    <!-- Database connection settings -->
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/your\_database</property>
    <property name="hibernate.connection.username">your\_username</property>
    <property name="hibernate.connection.password">your\_password</property>
    
    <!-- JDBC driver class -->
    <property name="hibernate.connection.driver\_class">com.mysql.cj.jdbc.Driver</property>
    
    <!-- Show SQL in console -->
    <property name="hibernate.show\_sql">true</property>
    
    <!-- Format SQL -->
    <property name="hibernate.format\_sql">true</property>
    
    <!-- Dialect -->
    <property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property>
    
    <!-- Automatic schema update -->
    <property name="hibernate.hbm2ddl.auto">update</property>
    
</session-factory>

Step 3: Create an Entity Class

Here is a simple entity class example:

import javax.persistence.Entity; import javax.persistence.Id;

@Entity public class Employee {

@Id
private int id;
private String name;
private String department;

// Getters and setters

}

Step 4: Set Up Hibernate SessionFactory

Configure the SessionFactory using the below Java code:

import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration;

public class HibernateUtil {

private static final SessionFactory sessionFactory;

static {
    try {
        sessionFactory = new Configuration()
                                .configure("hibernate.cfg.xml")
                                .addAnnotatedClass(Employee.class)
                                .buildSessionFactory();
    } catch (Throwable ex) {
        throw new ExceptionInInitializerError(ex);
    }
}

public static SessionFactory getSessionFactory() {
    return sessionFactory;
}

}

Step 5: Perform CRUD Operations

Use the Session object from SessionFactory to perform database operations. Here’s a simple example for saving an Employee:

import org.hibernate.Session; import org.hibernate.Transaction;

public class App { public static void main(String[] args) {

    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction transaction = session.beginTransaction();
    
    Employee employee = new Employee();
    employee.setId(1001);
    employee.setName("John Doe");
    employee.setDepartment("Engineering");
    
    session.save(employee);
    
    transaction.commit();
    
    session.close();
}

}

Additional Resources

For a deeper dive into Hibernate, consider exploring these resources:

Conclusion

By following these steps, you can successfully set up Hibernate with a MySQL database in 2026. Leveraging Hibernate’s abilities, you can efficiently handle database operations in a Java application. Regularly check for updates in Hibernate and MySQL versions to ensure your applications are running optimally.