Saturday , April 29 2017
Home / Spring Boot / Spring Boot Hibernate Integration Example

Spring Boot Hibernate Integration Example

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

Spring Boot Hibernate CRUD Application

Spring Boot Hibernate Integration :

The technologies which are used for this example.

  • 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.

<project xmlns="" xmlns:xsi=""

<!-- 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).


Create 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 connection to database, and it also consists 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 the main configuration file which is used to create datasource, 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 executed by the Spring boot while its loading..

@Configuration is an annotation, you can get more about @Configuration and @Bean here..

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

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

Create 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 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<Person> getAllPersons() {
        return getSession().createQuery("from Person").list();

Create 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<Person> getAllPersons() {
        try {
            return personDao.getAllPersons();
        } catch (Exception ex) {
            return null;

Create 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 are done with Spring Boot Hibernate example. Just we need to run class now.  If every thing went well you can 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

About chandrashekhar

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Leave a Reply

Your email address will not be published. Required fields are marked *