Hibernate SessionFactory with Spring Boot Example

This tutorial will guide you through setting up and demonstrating the use of SessionFactory in Hibernate with Spring Boot using a MySQL database. We will use the Employee entity for this example.


Spring Boot makes it easy to create stand-alone, production-grade Spring-based applications. By integrating Hibernate with Spring Boot, you can leverage the powerful ORM capabilities of Hibernate with the ease of configuration provided by Spring Boot. The SessionFactory in Hibernate is used to create sessions that manage database operations.

In this tutorial, we will:

  1. Set up a Spring Boot project with Hibernate and MySQL dependencies.
  2. Configure Hibernate in a Spring Boot application.
  3. Create an entity class (Employee).
  4. Implement an example to demonstrate the use of SessionFactory in Spring Boot.
  5. Demonstrate the SessionFactory usage using a sample application.

Step 1: Set Up Your Spring Boot Project

1.1 Create a Spring Boot Project

You can create a Spring Boot project using Spring Initializr or your IDE's Spring Boot project creation feature.

1.2 Add Dependencies

Update your pom.xml file to include the necessary dependencies for Spring Boot, Hibernate, and MySQL.

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">


        <!-- Spring Boot Starter Data JPA -->

        <!-- MySQL Connector -->

        <!-- Spring Boot Starter Web -->

        <!-- Spring Boot Starter Test -->


Step 2: Configure Hibernate in Spring Boot

2.1 Create application.properties

Create an application.properties file in the src/main/resources directory to configure database connection settings and Hibernate properties.

# MySQL Database Configuration

# Hibernate Configuration

Replace hibernate_db, root, and password with your MySQL database name and credentials.

2.2 Create a Configuration Class

Create a configuration class to define the SessionFactory bean.

package com.example.config;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;

import javax.sql.DataSource;
import java.util.Properties;

public class HibernateConfig {

    private DataSource dataSource;

    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        return sessionFactory;

    private Properties hibernateProperties() {
        Properties properties = new Properties();
        properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
        properties.put("hibernate.show_sql", "true");
        properties.put("hibernate.format_sql", "true");
        properties.put("hibernate.hbm2ddl.auto", "update");
        return properties;


  • The sessionFactory method creates and configures a LocalSessionFactoryBean which provides a SessionFactory.
  • The hibernateProperties method sets additional Hibernate properties.

Step 3: Create the Entity Class

Create an entity class Employee that will be mapped to a table in the database. This class uses annotations to define the entity and its fields.

package com.example.entity;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;

public class Employee {
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private String department;
    private double salary;

    // 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 getDepartment() {
        return department;

    public void setDepartment(String department) {
        this.department = department;

    public double getSalary() {
        return salary;

    public void setSalary(double salary) {
        this.salary = salary;


  • The @Entity annotation specifies that the class is an entity and is mapped to a database table.
  • The @Id annotation specifies the primary key of the entity.
  • The @GeneratedValue(strategy = GenerationType.IDENTITY) annotation specifies that the primary key is auto-incremented.

Step 4: Demonstrate the Use of SessionFactory

Create a MainApp class to demonstrate the use of SessionFactory. This class initializes the SessionFactory, opens a session, performs an operation, and then closes the session and shuts down the SessionFactory.

4.1 Create a Service Class

Create a service class to demonstrate the use of SessionFactory.

package com.example.service;

import com.example.entity.Employee;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

public class EmployeeService {

    private SessionFactory sessionFactory;

    public void saveEmployee(Employee employee) {
        Session session = sessionFactory.getCurrentSession();


  • The EmployeeService class demonstrates the use of SessionFactory to obtain a Session and perform an operation.
  • The saveEmployee method uses the Session to save an Employee entity.

4.2 Create a Controller Class

Create a controller class to expose an endpoint to demonstrate the use of SessionFactory.

package com.example.controller;

import com.example.entity.Employee;
import com.example.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

public class EmployeeController {

    private EmployeeService employeeService;

    public String saveEmployee(@RequestParam String name, @RequestParam String department, @RequestParam double salary) {
        Employee employee = new Employee();
        return "Employee saved successfully";


  • The EmployeeController class exposes an endpoint to save an employee.
  • The /saveEmployee endpoint takes name, department, and salary as request parameters and calls the saveEmployee method of EmployeeService.

4.3 Run the Application

Create a main application class to run the Spring Boot application.

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class HibernateSessionFactorySpringBootApplication {

    public static void main(String[] args) {
        SpringApplication.run(HibernateSessionFactorySpringBootApplication.class, args);


  • The HibernateSessionFactorySpringBootApplication class is the entry point of the Spring Boot application.
  • The main method runs the Spring Boot application.

Step 5: Test the Application

Run the Spring Boot application and test the /saveEmployee endpoint using a tool like Postman or a web browser. For example:


Sample Output

When you access the

endpoint, you should see the following response:

Employee saved successfully

This output indicates that the employee was successfully saved using Hibernate's SessionFactory.


In this tutorial, we have successfully demonstrated how to use SessionFactory in a Spring Boot application with Hibernate. We set up a Spring Boot project, configured Hibernate, created an entity class, implemented a service class to demonstrate the use of SessionFactory, and demonstrated the usage with a sample application. This guide provides a solid foundation for managing Hibernate sessions using SessionFactory in your Spring Boot applications.