JPA Repository is mainly used for managing the data in a Spring Boot Application. 

We all know that Spring is considered to be a very famous framework of Java. We mainly use this Spring Boot to create the Spring-based stand-alone and production-based applications with a very minimal amount of effort.

Post Graduate Program: Full Stack Web Development

in Collaboration with Caltech CTMEEnroll Now
Post Graduate Program: Full Stack Web Development

Spring Data 

Spring Data is considered a Spring based programming model for accessing data. A huge amount of code is required for working with the databases, which can be easily reduced by Spring Data. It consists of multiple modules. There are many Spring applications that use JPA technology, so these development procedures can be easily simplified by Spring Data JPA.

For each of the domain entities in the application, we define a repository interface with Spring Data. A repository includes all the methods such as sorting, paginating data and CRUD operations. For specifying that the underlying interface is a repository, a marker annotation @Repository is used.

A repository is created by extending particular repository interfaces like CrudRepository, PagingOrSorting Repository, or JPARepository.

Spring Data has some advanced integrations with MVC controllers and derives dynamic query from repository method names.


JpaRepository is particularly a JPA specific extension for Repository. It has full API CrudRepository and PagingAndSortingRepository. So, basically, Jpa Repository contains the APIs for basic CRUD operations, the APIS for pagination, and the APIs for sorting.

Example of Spring Boot JPARepository 

For managing an Office entity with a JPARepository, the following Spring application is used. The following program is mainly a console program, and the data is saved in the H2 database.

Let’s check out the structure of our project:




│   ├───java

│   │   └───com

│   │       └───springBootJPARepository

│   │           │

│   │           │

│   │           ├───model

│   │           │

│   │           └───repository

│   │         

│   └───resources




The following code is for the build file of Maven. For using Spring Data JPA with Hibernate, the spring-boot-starter-data is a starter.

New Course: Full Stack Development for Beginners

Learn Git Command, Angular, NodeJS, Maven & MoreEnroll Now
New Course: Full Stack Development for Beginners

Now let us go through the flow of the code of pom.xml:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns=""






































The file is considered to be the main Spring Boot configuration file. The Spring banner is turned off with the spring.main.banner-mode property.

For the console, the logging pattern is defined with logging.pattern.console.

Full Stack Web Developer Course

To become an expert in MEAN StackView Course
Full Stack Web Developer Course

Below is code for resources/


logging.pattern.console=%clr(%d{yy-MM-dd E HH:mm:ss.SSS}){blue} %clr(%-7p) %clr(%logger{0}){blue} %clr(%m){faint}%n

Now let’s check out the office entity. It consists attributes like office_id, office_name and no_of_employees.

Here is the code for com/springBootJPARepository/model/

package com.springBootJPARepository.model;

import java.util.Objects;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.GenerationType;

import javax.persistence.Id;

import javax.persistence.Table;


@Table(name = "offices")

public class Office {


    @GeneratedValue(strategy = GenerationType.AUTO)

    private Long office_id;

    private String office_name;

    private int no_of_employees;

    public Office() {


    public Office(String office_name, int no_of_employees) {

        this.office_name = office_name;

        this.no_of_employees = no_of_employees;


    public Long getOffice_id() {

        return office_id;


    public void setId(Long office_id) {

        this.office_id = office_id;


    public String getOffice_name() {

        return office_name;


    public void setOffice_name(String office_name) {

        this.office_name = office_name;


    public int getNo_of_employees() {

        return no_of_employees;


    public void setNo_of_employees(int no_of_employees) {

        this.no_of_employees = no_of_employees;



    public int hashCode() {

        int hash = 7;

        hash = 79 * hash + Objects.hashCode(this.office_id);

        hash = 79 * hash + Objects.hashCode(this.office_name);

        hash = 79 * hash + this.no_of_employees;

        return hash;



    public boolean equals(Object obj) {

        if (this == obj) {

            return true;


        if (obj == null) {

            return false;


        if (getClass() != obj.getClass()) {

            return false;


        final Office other = (Office) obj;

        if (this.no_of_employees != other.no_of_employees) {

            return false;


        if (!Objects.equals(this.office_name, other.office_name)) {

            return false;


        return Objects.equals(this.office_id, other.office_id);



    public String toString() {

        var builder = new StringBuilder();

        builder.append("Office{id=").append(office_id).append(", office_name=")

                .append(office_name).append(", no_of_employees=")


        return builder.toString();



From the JPARepository, the OfficeRepository gets extended; the type of entity and the type of primary key are provided by this repository.

Introduction to Java Spring framework 101

Learn AOP in Java, Spring MVC & Spring JDBCEnroll Now
Introduction to Java Spring framework 101

Now let us check out the code for which is in com/springBootJPARepository/repository/

package com.springBootJPARepository.repository;

import com.springBootJPARepository.model.Office;


import org.springframework.stereotype.Repository;


public interface OfficeRepository extends JpaRepository<City, Long> {


The various methods of JPARepository are used in

Let us check out the code in com/springBootJPARepository/

package com.springBootJPARepository;

import com.springBootJPARepository.model.Office;

import com.springBootJPARepository.repository.OfficeRepository;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.CommandLineRunner;


import org.springframework.stereotype.Component;


public class MyRunner implements CommandLineRunner {

    private static final Logger logger = LoggerFactory.getLogger(MyRunner.class);


    private OfficeRepository OfficeRepository;


    public void run(String... args) throws Exception { Office("A COMPNAY", 432000)); Office("B COMPANY", 1759000)); Office("C COMPANY", 1280000)); Office("D COMPANY", 1748000)); Office("E COMPANY", 3971000)); Office("F COMPANY", 8550000)); Office("G COMPANY", 464000));"# of offices: {}", OfficeRepository.count());"All offices unsorted:");

        var offices = OfficeRepository.findAll();"{}", offices);"------------------------");"All offices sorted by name_of_office in descending order");

        var sortedoffices = OfficeRepository.findAll(, "name_of_office"));"{}", sortedoffices);"------------------------");"Deleting all offices");

        OfficeRepository.deleteAllInBatch();"# of offices: {}", OfficeRepository.count());



We then inject the OfficeRepocitory into the OfficeRepository field.


private OfficeRepository OfficeRepository;

With save(), a new office is inserted: Office("A COMPANY", 432000));

With count(), the number of cities are counted:"# of offices: {}", officeRepository.count());

For getting all the offices, findAll() method is generally used:"All offices unsorted:");

var offices = officeRepository.findAll();"{}", offices);

To the findAll(), a sort object is passed so that we can get all the names of the cities in descending order:"All offices sorted by name_of_office in descending order");

var sortedoffices = cityRepository.findAll(, "name_of_office"));"{}", sortedoffices);

In a batch, all the cities can be deleted by us using deleteAllInBatch()."Deleting all offices");


The spring boot application is set up by the Spring Boot Application.

Let’s first check out the code for com/springBootJPARepository/

package com.springBootJPARepository;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication; 


public class Application {

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




22-02-15 Tue 12:47:14.593 INFO  MyRunner # of offices: 7

22-02-15 Tue 12:47:14.593 INFO  MyRunner All offices unsorted:

22-02-15 Tue 12:47:14.652 INFO  MyRunner [Office{id_of_office=1, name_of_office=A COMPANY, population=432000}, Office{id_of_office=2, name_of_office=B COMPANY, population=1759000}, Office{id_of_office=3, name_of_office=C COMPANY, population=1280000}, Office{id_of_office=4, name_of_office=D COMPANY, population=1748000}, City{id_of_office=5, name_of_office=E COMPANY, population=3971000}, City{id_of_office=6, name_of_office=F COMPANY, population=8550000}, City{id_of_office=7, name_of_office=G COMPANY, population=464000}]

22-02-15 Tue 12:47:14.652 INFO  MyRunner ------------------------

22-02-15 Tue 12:47:14.652 INFO  MyRunner All offices sorted by name_of_office in descending order

22-02-15 Tue 12:47:14.667 INFO  MyRunner [City{id_of_office=7, name_of_office=G COMPANY, population=1748000}, City{id_of_office=6, name_of_office=F COMPANY, population=1280000}, City{id_of_office=5, name_of_office=E COMPANY, population=8550000}, City{id_of_office=4, name_of_office=D COMPANY, population=3971000}, City{id_of_office=3, name_of_office=C COMPANY, population=464000}, City{id_of_office=2, name_of_office=B COMPANY, population=1759000}, City{id_of_office=1, name_of_office=A COMPANY, population=432000}]

22-02-15 Tue 12:47:14.668 INFO  MyRunner ------------------------

22-02-15 Tue 12:47:14.668 INFO  MyRunner Deleting all offices

22-02-15 Tue 12:47:14.681 INFO  MyRunner # of offices: 0


Master front-end and back-end technologies and advanced aspects in our Post Graduate Program in Full Stack Web Development. Unleash your career as an expert full stack developer. Get in touch with us NOW!


In this article, we discovered how we can manage our application with JpaRepository.

As we know, these days along with the knowledge of data structure and algorithms, development skill is also tested by recruiters while hiring. A techie should therefore be closely conversant with all the technologies being used by companies and keep track of the latest developments and trends of the tech industry. There are some full stack development courses offered by Simplilearn you can check out for upgrading your skill set. This program will give you the foundation for building full stack web apps using the Java programming language. You'll begin with the basics of JavaScript, and then venture into some of the more advanced concepts like Angular, Spring Boot, Hibernate, JSPs, and MVC.

Another such platform is SkillUp where all of the self-learning courses are free of cost, so you can explore and learn in-demand skills in different domains of your choice such as Machine Learning, Artificial Intelligence, business analytics etc. on your schedule.

Enroll in one of these programs to enhance your skills and become industry-ready.

About the Author


Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.