How to Integrate Spring Boot with Hibernate

In this article of Spring Boot, we will discuss as how to integrate Spring Boot with Hibernate. We will build a simple Spring Boot application and use Hibernate to store the data.

1. Spring Boot with Hibernate

To bootstrap our application, we have the following options

  1. Spring Initializr
  2. Use IDE
  3. Use Spring Boot CLI tool.

We are using the Spring Initializr for this article. As part of the setup, we will add, Web, JPA, MySQL dependencies our application. To start, go to the Spring Initializr and create the project structure.

Spring Boot with Hibernate

Click on the “Generate” button to download the project structure on your machine. This is how our pom.xml will look like

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi="" xsi:schemaLocation="">
      <relativePath />
      <!-- lookup parent from repository -->
   <description>Application to integrate Spring Boot with Hibernate</description>

We are using MySQL for this post, in case you like to use other DB, please add the correct dependencies in your pom.xml file. With these dependencies, Spring Boot ensure to include all the jars in the class-path including the Hibernate. Here is the screenshot of the project structure.

Spring Boot with Hibernate

2. Configuring Database (MySQL)

Spring Boot will automatically try to create DataSource if it finds the if spring-data-jpa is in the class-path. It will try to read the database configurations from the file. Let’s add the database configuration to our file and let Spring Boot to do the rest of work for us.

#datasource configurations

# DDL generation

Create the database with name spring-boot-hibernate.

3. Database Initialization

Spring Boot JPA provides the option for DDL generation. Spring JPA provides following two properties for the DDL generation.

  1. spring.jpa.generate-ddl (boolean) as vendor independent.
  2. spring.jpa.hibernate.ddl-auto Hibernate specific feature.

For more information on Hibernate property, please read Initialize a Database Using Hibernate

4. Create JPA Entity Class

To use the hibernate to work with our data, let’s create a customer entity to store and retrieve customer information from the database.

public class Customer {

 @GeneratedValue(strategy = GenerationType.IDENTITY)
 private Long id;
 private String firstName;
 private String lastName;
 private String email;

 public Customer() {
 //get & set methods

Let’s cover some important points from our entity class:

  1. The @Entity annotation show that this class is an entity class.
  2. We are not using @Table annotation, Hibernate will map it to the table named as “Customer“. You can use a different table name with @Table(name = "User")
  3. The @Id annotation mark the id field as unique id field. We are using the identity generation strategy for our example.
  4. The firstName and lastName are un-mapped and it will map them to the same column name as in the properties. We have the option to use @Column annotation to customize column names.

5. Create The Repository

Spring JPA provide a transparent integration with the underlying JPA layer using the JPA repositories. It will take care of creating repository implementations automatically, at run-time, from a repository interface. Let’s create a simple CustomerRepository for our Customer entity and let Spring JPA handle the implementation part for us:

public interface CustomerRepository extends JpaRepository<Customer,Long> { }

Spring JPA handle most of the work for us and we only need to define the interface.The @Repository annotation helps Spring to bootstrap the JPA features during component scanning.The JpaRepository provide several OOTB methods to help us get started.

  1. save(S entity)
  2. findById(ID id)
  3. findOne()
  4. findAll()

For a complete list of method, please refer to the SimpleJpaRepository

6. Create The Service

Let’s create a simple service which will interact with the JPA repository to perform database operations:

package com.javadevjournal.service;

import com.javadevjournal.dto.CustomerData;
import com.javadevjournal.repository.CustomerRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityNotFoundException;
import java.util.ArrayList;
import java.util.List;

public class DefaultCustomerService implements CustomerService {

    private CustomerRepository customerRepository;

     * Create a customer based on the data sent to the service class.
     * @param customer
     * @return DTO representation of the customer
    public CustomerData saveCustomer(CustomerData customer) {
        Customer customerModel = populateCustomerEntity(customer);
        return populateCustomerData(;

     * Delete customer based on the customer ID.We can also use other option to delete customer
     * based on the entity (passing JPA entity class as method parameter)
     * @param customerId
     * @return boolean flag showing the request status
    public boolean deleteCustomer(Long customerId) {
        retunr true;

     * Method to return the list of all the customers in the system.This is a simple
     * implementation but use pagination in the real world example.
     * @return list of customer
    public List < CustomerData > getAllCustomers() {
        List < CustomerData > customers = new ArrayList < > ();
        List < Customer > customerList = customerRepository.findAll();
        customerList.forEach(customer - > {
        return customers;

     * Get customer by ID. The service will send the customer data else will throw the exception. 
     * @param customerId
     * @return CustomerData
    public CustomerData getCustomerById(Long customerId) {
        return populateCustomerData(customerRepository.findById(customerId).orElseThrow(() - > new EntityNotFoundException("Customer not found")));

     * Internal method to convert Customer JPA entity to the DTO object
     * for frontend data
     * @param customer
     * @return CustomerData
    private CustomerData populateCustomerData(final Customer customer) {
        CustomerData customerData = new CustomerData();
        return customerData;

     * Method to map the front end customer object to the JPA customer entity.
     * @param customerData
     * @return Customer
    private Customer populateCustomerEntity(CustomerData customerData) {
        Customer customer = new Customer();
        return customer;

Our service class provides method to save, delete and find all customers in the database.

7. Create Customer Controller

package com.javadevjournal.controller;

import com.javadevjournal.dto.CustomerData;
import com.javadevjournal.service.CustomerService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

public class CustomerController {

    @Resource(name = "customerService")
    private CustomerService customerService;

     * <p>Get all customer data in the system.For production system you many want to use
     * pagination.</p>
     * @return List<CustomerData>
    public List < CustomerData > getCustomers() {
        return customerService.getAllCustomers();

     * Method to get the customer data based on the ID.
     * @param id
     * @return CustomerData
    public CustomerData getCustomer(@PathVariable Long id) {
        return customerService.getCustomerById(id);

     * Post request to create customer information int the system.
     * @param customerData
     * @return
    public CustomerData saveCustomer(final @RequestBody CustomerData customerData) {
        return customerService.saveCustomer(customerData);

     * <p>Delete customer from the system based on the ID. The method mapping is like the getCustomer with difference of
     * @DeleteMapping and @GetMapping</p>
     * @param id
     * @return
    public Boolean deleteCustomer(@PathVariable Long id) {
        return customerService.deleteCustomer(id);


8. Test The Application

As part of the last step, let’s create the main class for our Spring Boot application:

public class SpringBootHibernateApplication {

 private static final Logger log = LoggerFactory.getLogger(SpringBootHibernateApplication.class);

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

We will create few records in the system using the POST method. We are using the Postman for this demo but you can use any other REST client.

Spring Boot with Hibernate- Create User
Retrieving all customers from the system GET /customers
Spring Boot Hibernate - Get All Customers
Retrieving customer information from the system by customer id GET /customers/customer/2
Spring Boot Hibernate - Get Customer
Delete customer information from by customer id DELETE /customers/customer/2
Spring Boot Hibernate - Delete Customer
Updated Customer List
Spring Boot Hibernate - All Customers

9. Enable JPA Auditing

Spring Data provides sophisticated support to transparently keep track of who created or changed an entity and the point in time this happened. To enjoy that functionality you have to equip your entity classes with auditing metadata that can be defined either using annotations or by implementing an interface.

Spring JPA provides @CreatedBy, @LastModifiedBy to capture the user who created or changed the entity and @CreatedDate and @LastModifiedDate to capture the point in time this happened. Here is our changed JPA entity class with the auditing changes.

public class Customer {

    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String firstName;
    private String lastName;
    private String email;

    @Column(updatable = false, nullable = false)
    private LocalDateTime createdTime;

    @Column(nullable = false)
    private LocalDateTime updatedTime;

    //getter and setter

Apart from the annotation on the createdTime and modifiedTime fields, we need to perform the following changes

  • Add the AuditingEntityListener to the domain model. We added the @EntityListeners(AuditingEntityListener.class) to our JPA entity class.
  • Enable the JPA auditing in the main class using the @EnableJpaAuditing annotation.

10. Enable SQL Logging

In the development phase, it’s helpful to see the SQL statements being generated by Hibernate.We can enable it using the following properties in our file

#Statitcs and SQL logs

#Enable this section based on your preferences

logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} - %msg%n

Above setting will enable the SQL statement printing on the console.if we call our controller method to get the list of customers, we will also find the following SQL statement printing on the console:

    select as id1_0_,
        customer0_.created_time as created_2_0_, as email3_0_,
        customer0_.first_name as first_na4_0_,
        customer0_.last_name as last_nam5_0_,
        customer0_.updated_time as updated_6_0_ 
        customer customer0_
2020-02-12 21:44:40 - Session Metrics {
    939620 nanoseconds spent acquiring 1 JDBC connections;
    0 nanoseconds spent releasing 0 JDBC connections;
    23985787 nanoseconds spent preparing 1 JDBC statements;
    1151373 nanoseconds spent executing 1 JDBC statements;
    0 nanoseconds spent executing 0 JDBC batches;
    0 nanoseconds spent performing 0 L2C puts;
    0 nanoseconds spent performing 0 L2C hits;
    0 nanoseconds spent performing 0 L2C misses;
    0 nanoseconds spent executing 0 flushes (flushing a total of 0 entities and 0 collections);
    28096 nanoseconds spent executing 1 partial-flushes (flushing a total of 0 entities and 0 collections)

The first part displays the SQL being generated by system while the second part shows the metrics 

11. Table Names and Uppercase

In case you to set the database table name in Upper case, you many need to set the spring.jpa.hibernate.naming.physical-strateg property in your file and set the entity name accordingly. Let’s see one example configuration for better understanding.

Set the following property in the or yml file.


You can now configure the entity name as:

public class Customer {
 //member details


In this article, we learned how to integrate Spring Boot with Hibernate. We saw how Spring Boot transparently handle the JPA integration including Hibernate. The source code for this article is available on the GitHub.

Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments