Spring Boot – Integrating Hibernate and JPA
Last Updated :
03 Jan, 2022
Spring Boot is built on the top of the spring and contains all the features of spring. And is becoming a favorite of developers these days because of its rapid production-ready environment which enables the developers to directly focus on the logic instead of struggling with the configuration and setup. Spring Boot is a microservice-based framework and making a production-ready application in it takes very little time.
In this article we learn:
- What are JPA and Hibernate?
- How does JPA Work?
- How to integrate the SpringBoot project with Hibernate and JPA.
JPA(Java Persistence API): JPA is like a bridge between the Spring application Models and the relational database that is used for managing and accessing the data between the Object-Oriented models of the Spring application and the database. In simple terms, we have to define all methods for inserting the records in the MySQL table if we don’t use the JPA. JPA doesn’t perform any kind of specific operations it provides various types of methods without implementation, just like an interface that contains various methods like counting the records, deleting the specific record. etc.
Hibernate is a java framework and ORM (Object Relation Mapping) tool that is used to provide the implementation of the JPA methods.
How does JPA Work?
JPA is an abstraction that is used to map the java object with the database. It contains different structures of the methods that are used for manipulating the table record and also provides the SQL queries for specific operations.
How to Integrate the SpringBoot project with Hibernate and JPA
Step By Step Implementation
- Go to Spring Initializr
- Fill in the details as per the requirements
- Click on Generate which will download the starter project
- Extract the zip file
Create a project using the spring initializer.

pom.xml file:
XML
<? xml version = "1.0" encoding = "UTF-8" ?>
< modelVersion >4.0.0</ modelVersion >
< parent >
< groupId >org.springframework.boot</ groupId >
< artifactId >spring-boot-starter-parent</ artifactId >
< version >2.6.1</ version >
< relativePath />
</ parent >
< groupId >com.example</ groupId >
< artifactId >SpringBootApp</ artifactId >
< version >0.0.1-SNAPSHOT</ version >
< name >SpringBootApp</ name >
< description >Demo project for Spring Boot</ description >
< properties >
< java.version >11</ java.version >
</ properties >
< dependencies >
< dependency >
< groupId >org.springframework.boot</ groupId >
< artifactId >spring-boot-starter-data-jpa</ artifactId >
</ dependency >
< dependency >
< groupId >org.springframework.boot</ groupId >
< artifactId >spring-boot-starter-web</ artifactId >
</ dependency >
< dependency >
< groupId >mysql</ groupId >
< artifactId >mysql-connector-java</ artifactId >
< scope >runtime</ scope >
</ dependency >
< dependency >
< groupId >org.springframework.boot</ groupId >
< artifactId >spring-boot-starter-test</ artifactId >
< scope >test</ scope >
</ dependency >
</ dependencies >
< build >
< plugins >
< plugin >
< groupId >org.springframework.boot</ groupId >
< artifactId >spring-boot-maven-plugin</ artifactId >
</ plugin >
</ plugins >
</ build >
</ project >
|
Extract the zip file. Now open a suitable IDE and then go to File -> New -> Project from existing sources -> Springbootapp and select pom.xml. Click on import changes on prompt and wait for the project to sync.

Note: In the Import Project for Maven window, make sure you choose the same version of JDK which you selected while creating the project.
For database operation we have to configure the Spring application with the database also it is required to add configuration of the database before executing the Spring project. If we try to run the Spring application that contains JPA dependency with adding the configuration of the database it causes an error. Running the project without database configuration:
Run the SpringBootAppApplication:

Configuring the Spring application with the database:
application.properties file:
spring.datasource.url=jdbc:mysql://localhost:3306/user
spring.datasource.username=root
spring.datasource.password=Aayush
spring.jpa.hibernate.ddl-auto=update
Now run the Spring Boot application after configuration these properties:

Similar Reads
Spring Boot - AOP After Throwing Advice
Spring is widely used for creating scalable applications. For web applications Spring provides. Spring MVC is a widely used module of spring that is used to create scalable web applications. While Aspect-oriented programming(AOP) as the name suggests uses aspects in programming. It can be defined as
6 min read
Spring Boot - AOP After Returning Advice
Prerequisite: Aspect Oriented Programming and AOP in Spring Framework Aspect-oriented programming(AOP) as the name suggests uses aspects in programming. It can be defined as the breaking of code into different modules, also known as modularisation, where the aspect is the key unit of modularity. Asp
5 min read
Multi-Module Project With Spring Boot
Multi-Module project with Spring Boot refers to a project structure where multiple modules or subprojects are organized under a single parent project. Each module can represent a distinct component, functionality, or layer of the application, allowing for better organization, maintainability, and co
6 min read
Spring Boot - AOP After Advice
Prerequisite: Aspect Oriented Programming and AOP in Spring Framework Aspect-oriented programming(AOP) as the name suggests uses aspects in programming. It can be defined as the breaking of code into different modules, also known as modularisation, where the aspect is the key unit of modularity. Asp
5 min read
Spring Boot - AOP Before Advice
Aspect-oriented programming(AOP) as the name suggests uses aspects in programming. It can be defined as the breaking of code into different modules, also known as modularisation, where the aspect is the key unit of modularity. Aspects enable the implementation of crosscutting concerns such as transa
4 min read
Spring Boot - DevTools
Spring Boot provides DevTools, a module designed to ease your development process, it improves the experience of developing applications resulting in optimizing the developer's productivity for building exceptional software. It aims to reduce development time, by intelligently detecting code changes
5 min read
Spring Boot - Dependency Management
Spring Boot framework is the most popular web development framework. No doubt, it provides an abundance of essential features and a convenient way to handle those features. At the heart of Spring Boot is the 'Dependency Management' feature. Importance of Dependency ManagementCentralized Dependency M
6 min read
Spring Boot - Caching
Spring Boot is a project that is built on top of the Spring Framework that provides an easier and faster way to set up, configure, and run both simple and web-based applications. It is one of the popular frameworks among developers these days because of its rapid production-ready environment which e
6 min read
Spring Boot - Starter Web
Today most of the applications demand Model-View-Controller (MVC) architecture to meet the various needs like handling user data, making application efficient, to provide dynamic nature to applications. It was basically used for building desktop graphical user interfaces (GUIs), but now is becoming
6 min read
Spring Boot - Difference Between @Service Annotation and @Repository Annotation
Spring Annotations are a form of metadata that provides data about a program. Annotations are used to provide supplemental information about a program. It does not have a direct effect on the operation of the code they annotate. It does not change the action of the compiled program. @Service Annotat
7 min read