In this tutorial, we are going to show a simple Spring Boot with Hibernate Example. The Spring Boot Hibernate integration is a crazy combination since Hibernate has its own importance.

Spring Boot Hibernate Integration :


  • Spring Boot 1.2.3.RELEASE
  • Java 1.7
  • Hibernate 4.3
  • Maven
  • MySql

Spring Boot Hibernate Project Structure :

A typical Maven project structure.

Spring Boot Hibernate

Project Dependencies:


Here the main important thing is to place the spring-boot-starter-data-jpa dependency; it will take all the internal dependencies.

Recommended: Spring Boot with JPA Integration

<project xmlns="" xmlns:xsi="" xsi:schemaLocation=""> 
    <properties> <!-- Generic properties --> 
        <relativePath /> 

Database Schema :

Create a person table in your database, since we are going to access this from our application ( Spring Boot Hibernate integration).

Database Table

Create an Entity Class for person table.
package com.onlinetutorialspoint.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Table(name = "person")
public class Person {
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    @Column(name = "pcity")
    private String city;

    public Person() {

    public Long getId() {
        return id;

    public void setId(Long id) { = id;

    public String getName() {
        return name;

    public void setName(String name) { = name;

    public String getCity() {
        return city;

    public void setCity(String city) { = city;

    public String toString() {
        return "Person [pid=" + id + ", pName=" + name + ", pCity=" + city
                + "]";


There is no special in class as part of Spring Boot Hibernate. It is as simple as a simple hibernate application entity file.

Configuration Properties :

Configuration information to get a connection from the database, and it is also consists of hibernate configurations like hibernate hbm2ddl auto.

Properties File :
# Database
db.driver: com.mysql.jdbc.Driver
db.url: jdbc:mysql://localhost:3306/onlinetutorialspoint
db.username: root
db.password: 12345

# Hibernate
hibernate.dialect: org.hibernate.dialect.MySQL5Dialect
hibernate.show_sql: true create
entitymanager.packagesToScan: com

Create a DBConfiguration:

As part of the Spring Boot Hibernate integration, this is the main configuration file which is used to create a data source, Hibernate session Factory and managing transactions.

package com.onlinetutorialspoint.config;

import java.util.Properties;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

public class DBConfiguration {
    private String DRIVER;

    private String PASSWORD;

    private String URL;

    private String USERNAME;

    private String DIALECT;

    private String SHOW_SQL;

    private String HBM2DDL_AUTO;

    private String PACKAGES_TO_SCAN;

    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        return dataSource;

    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        Properties hibernateProperties = new Properties();
        hibernateProperties.put("hibernate.dialect", DIALECT);
        hibernateProperties.put("hibernate.show_sql", SHOW_SQL);
        hibernateProperties.put("", HBM2DDL_AUTO);

        return sessionFactory;

    public HibernateTransactionManager transactionManager() {
        HibernateTransactionManager transactionManager = new HibernateTransactionManager();
        return transactionManager;

The is a configuration file. Which will be executed by the Spring boot while it’s loading..

@Configuration annotation allows you to define configurations. you can get more about @Configuration and @Bean here.

@EnableTransactionManagement it enables the annotation-driven transaction management capability; we can also allow the transaction by using the <tx:*> XML namespace.

@Value is an annotation given by spring framework. It comes with Spring 3.0 release. @Value annotation is used for expression-driven dependency injection. A typical use case is to assign default field values using “${db.driver}” style expressions.

Create a DAO Class:

A class it performs basic crud operations. To make the Spring Boot Hibernate Example as simple as possible, I have created the method to get all persons from the database here.

package com.onlinetutorialspoint.dao;

import java.util.List;

import javax.transaction.Transactional;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.onlinetutorialspoint.model.Person;

public class PersonDAO {
    private SessionFactory sessionFactory;

    private Session getSession() {
        return sessionFactory.getCurrentSession();

    public String savePerson(Person person) {
        Long isSuccess = (Long)getSession().save(person);
        if(isSuccess >= 1){
            return "Success";
            return "Error while Saving Person";

    public boolean delete(Person person) {
        return true;

    public List getAllPersons() {
        return getSession().createQuery("from Person").list();

Create a Spring Controller :

package com.onlinetutorialspoint.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.onlinetutorialspoint.dao.PersonDAO;
import com.onlinetutorialspoint.model.Person;

@RequestMapping(value = "/person")
public class PersonController {
    private PersonDAO personDao;

    @RequestMapping(value = "/delete")
    public String delete(long id) {
        try {
            Person person = new Person();
        } catch (Exception ex) {
            return ex.getMessage();
        return "Person succesfully deleted!";

    @RequestMapping(value = "/save")
    public String create(String name, String city) {
        try {
            Person person = new Person();
        } catch (Exception ex) {
            return ex.getMessage();
        return "Person succesfully saved!";
    @RequestMapping(value = "/allPersons")
    public List getAllPersons() {
        try {
            return personDao.getAllPersons();
        } catch (Exception ex) {
            return null;

Create a Spring Boot Application Class :

package com.onlinetutorialspoint;

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

public class Application {
  public static void main(String[] args) {, args);


Well! We have done with Spring Boot Hibernate example. We need to run class now.  If everything went well, you could see the output log like below.

Spring Boot Hibernate Output

Now we can access the application by http://localhost:8080/person/

Insert the Person :

http://localhost:8080/person/save?name=chandra shekhar Goka&city=Hiderabad

Spring Boot Hibernate Save

Show All Persons :


Spring Boot Hibernate all

Delete a Person By Id :


Spring Boot Hibernate Delete

Happy Learning 🙂

Download Example