Spring Boot - Spring Data JPA
Last Updated :
03 Apr, 2024
Spring Data JPA or JPA stands for Java Persistence API, so before looking into that, we must know about ORM (Object Relation Mapping). So Object relation mapping is simply the process of persisting any java object directly into a database table. Usually, the name of the object being persisted becomes the name of the table, and each field within that object becomes a column. With the table setup, each row corresponds to a record in the application. Hibernate is one example of ORM. In short, JPA is the interface while hibernate is the implementation.
The java persistence API provides a specification for persisting, reading, and managing data from your java object to your relational tables in the database. JPA specifies the set of rules and guidelines for developing interfaces that follow standards. Straight to the point: JPA is just guidelines to implement ORM and there is no underlying code for the implementation. Spring Data JPA is part of the spring framework. The goal of spring data repository abstraction is to significantly reduce the amount of boilerplate code required to implement a data access layer for various persistence stores. Spring Data JPA is not a JPA provider, it is a library/framework that adds an extra layer of abstraction on the top of our JPA provider line Hibernate.
Configure Spring Data JPA in Spring Application with Example
Requirements: STS IDE, MySQL workbench, Java 8+
Create a spring boot project in STS. Give project name & select add required dependencies(Spring JPA, MySQL driver, Spring web) shown in attached pom.xml.
XML
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>ex</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ex</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>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</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>
OR you can also add these dependencies while creating the project.
application.properties file:
spring.datasource.url=jdbc:mysql://localhost:3306/emp
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform = org.hibernate.dialect.MySQL5Dialect
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto = update
Project Structure:

Modal layer:
Create a simple POJO(Plain old java class) with some JPA annotation.
- @Entity: This annotation defines that a class can be mapped to a table
- @Id: This annotation specifies the primary key of the entity.
- @GeneratedValue: This annotation is used to specify the primary key generation strategy to use. i.e. Instructs database to generate a value for this field automatically. If the strategy is not specified by default AUTO will be used.
Java
package com.example.demo.modal;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
// @Entity annotation defines that a
// class can be mapped to a table
@Entity
public class Employee {
// @ID This annotation specifies
// the primary key of the entity.
@Id
// @GeneratedValue This annotation
// is used to specify the primary
// key generation strategy to use
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String name;
private String city;
public Employee() {
super();
}
public Employee(String name, String city) {
super();
this.name = name;
this.city = city;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
DAO(Data access object) layer:
- @Repository: The @Repository annotation is a marker for any class that fulfills the role or stereotype of a repository (also known as Data Access Object or DAO).
- JpaRepository<Employee, Long> JpaRepository is a JPA-specific extension of the Repository. It contains the full API of CrudRepository and PagingAndSortingRepository. So it contains API for basic CRUD operations and also API for pagination and sorting. Here we enable database operations for Employees.
Java
package com.example.demo.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.example.demo.modal.Employee;
// @Repository is a Spring annotation that
// indicates that the decorated class is a repository.
@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long>{
ArrayList<Employee> findAllEmployee();
}
Service layer:
Java
package com.example.demo.service;
import java.util.ArrayList;
import com.example.demo.modal.Employee;
public interface EmpService {
ArrayList<Employee> findAllEmployee();
Employee findAllEmployeeByID(long id);
void addEmployee();
void deleteAllData();
}
@Service: This annotation is used with classes that provide some business functionalities. Spring context will autodetect these classes when annotation-based configuration and classpath scanning is used. Here JPA repository has lots of predefined generic methods to perform the database operation some are used in the below code.
Java
package com.example.demo.service;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.demo.modal.Employee;
import com.example.demo.repository.EmployeeRepository;
// @Service marks a Java class that performs some service,
// such as executing business logic, performing
// calculations, and calling external APIs.
@Service
public class EmpServiceImpl implements EmpService {
@Autowired
EmployeeRepository employeeRepository;
@Override
public ArrayList<Employee> findAllEmployee() {
return (ArrayList<Employee>) employeeRepository.findAll();
}
@Override
public Employee findAllEmployeeByID(long id) {
Optional<Employee> opt = employeeRepository.findById(id);
if (opt.isPresent())
return opt.get();
else
return null;
}
@Override
public void addEmployee() {
ArrayList<Employee> emp = new ArrayList<Employee>();
emp.add(new Employee("Lucknow", "Shubham"));
emp.add(new Employee("Delhi", "Puneet"));
emp.add(new Employee("Pune", "Abhay"));
emp.add(new Employee("Noida", "Anurag"));
for (Employee employee : emp) {
employeeRepository.save(employee);
}
}
@Override
public void deleteAllData() {
employeeRepository.deleteAll();
}
}
Controller layer:
- @RestController: This is a Spring annotation that is used to build REST API in a declarative way. RestController annotation is applied to a class to mark it as a request handler, and Spring will do the building and provide the RESTful web service at runtime.
- @Autowired: This annotation can be used to autowire bean on the setter method just like @Required annotation, constructor, a property, or methods with arbitrary names and/or multiple arguments.
- @PostMapping: This annotation maps HTTP POST requests onto specific handler methods. It is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod. POST)
- @GetMapping: This annotation is a specialized version of @RequestMapping annotation that acts as a shortcut for @RequestMapping(method = RequestMethod. GET). The @GetMapping annotated methods in the @Controller annotated classes handle the HTTP GET requests matched with the given URI expression.
- @DeleteMapping: This annotation maps HTTP DELETE requests onto specific handler methods. It is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod. DELETE)
Java
package com.example.demo.controller;
import java.util.ArrayList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.modal.Employee;
import com.example.demo.service.EmpServiceImpl;
@RestController
public class EmpController {
@Autowired
EmpServiceImpl empServiceImpl;
@PostMapping("/")
public void add() {
empServiceImpl.addEmployee();
}
@GetMapping("/findall")
public ArrayList<Employee> getAllEmployee() {
return empServiceImpl.findAllEmployee();
}
@GetMapping("/findbyid/{id}")
public Employee getEmployeeUsingId(@PathVariable long id) {
return empServiceImpl.findAllEmployeeByID(id);
}
@DeleteMapping("/delete")
public void delete() {
empServiceImpl.deleteAllData();
}
}
Display output using JPA in postman:
Open postman and hit the listed API one by one.
Save employee data

Find all employee list


Find an employee by id


Delete all employee
Similar Reads
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 Annota
7 min read
Unit Testing in Spring Boot Project using Mockito and Junit 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 se
6 min read
Spring Boot - Thymeleaf with Example Thymeleaf is a server-side Java-based template engine for both web and standalone environments, capable of processing HTML, XML, JavaScript, CSS and even plain text. It is more powerful than JPS and responsible for dynamic content rendering on UI. The engine allows a parallel work of the backend and
6 min read
Spring Boot - MongoRepository with Example 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 se
5 min read