CrudRepository vs PagingAndSortingRepository vs JpaRepository in Spring Data JPA

Spring Data JPA provides multiple repository interfaces to simplify database operations. One of the most common interview questions for Spring developers is understanding the difference between CrudRepository, PagingAndSortingRepository and JpaRepository.


Although these interfaces appear similar, they offer different capabilities and are designed for different use cases.


Repository Hierarchy

Before comparing these interfaces, it is important to understand their inheritance relationship.



Repository
     |
     v
CrudRepository
     |
     v
PagingAndSortingRepository
     |
     v
JpaRepository

Each interface extends the previous one and provides additional functionality.


What is Repository?

Repository is the base marker interface in Spring Data.



public interface Repository<T, ID> {

}

It does not provide any methods. It simply identifies an interface as a Spring Data repository.


CrudRepository

CrudRepository provides basic Create, Read, Update and Delete operations.



public interface EmployeeRepository
extends CrudRepository<
        Employee,
        Long> {

}

Common Methods


save()

findById()

findAll()

deleteById()

existsById()

count()

Example


Employee employee =
        new Employee(
            "John",
            "IT",
            50000.0
        );

repository.save(employee);

The save() method inserts or updates records.


When Should You Use CrudRepository?

  • Simple CRUD applications
  • Basic database operations
  • Small projects
  • Learning Spring Data JPA

PagingAndSortingRepository

PagingAndSortingRepository extends CrudRepository and adds pagination and sorting support.



public interface EmployeeRepository
extends PagingAndSortingRepository<
        Employee,
        Long> {

}

This repository is useful when working with large datasets.


Additional Methods


findAll(Pageable pageable)

findAll(Sort sort)

Pagination Example


Pageable pageable =
        PageRequest.of(
            0,
            5
        );

Page<Employee>
employees =
repository.findAll(pageable);

This fetches the first page containing five records.


Sorting Example


Sort sort =
        Sort.by("salary")
            .descending();

Iterable<Employee>
employees =
repository.findAll(sort);

Results are returned in descending salary order.


When Should You Use PagingAndSortingRepository?

  • Large datasets
  • Pagination requirements
  • Sorting requirements
  • REST APIs returning large results

JpaRepository

JpaRepository extends PagingAndSortingRepository and provides JPA-specific functionality.



public interface EmployeeRepository
extends JpaRepository<
        Employee,
        Long> {

}

This is the most commonly used repository interface in Spring Boot applications.


Additional Methods


flush()

saveAndFlush()

deleteAllInBatch()

deleteAllByIdInBatch()

getReferenceById()

saveAndFlush() Example


repository.saveAndFlush(
        employee
);

It immediately writes changes to the database.


Batch Delete Example


repository.deleteAllInBatch();

Deletes all records using batch operations.


Feature Comparison

Feature CrudRepository PagingAndSortingRepository JpaRepository
CRUD Operations Yes Yes Yes
Pagination No Yes Yes
Sorting No Yes Yes
Batch Operations No No Yes
Flush Support No No Yes

Which Repository Should You Use?

In modern Spring Boot applications, JpaRepository is usually the preferred choice because it includes all CRUD, pagination, sorting and JPA-specific features.

Most enterprise applications directly use JpaRepository instead of CrudRepository or PagingAndSortingRepository.


Real Project Example


@Repository
public interface EmployeeRepository
        extends JpaRepository<
            Employee,
            Long> {

}

This single interface provides CRUD, paging, sorting and batch operations.


Common Interview Questions

Does JpaRepository extend CrudRepository?

Yes. JpaRepository indirectly extends CrudRepository through PagingAndSortingRepository.


Which repository supports pagination?

PagingAndSortingRepository and JpaRepository.


Which repository is most commonly used?

JpaRepository.


What is the advantage of JpaRepository?

It provides CRUD, pagination, sorting and additional JPA-specific operations.


Best Practices

  • Prefer JpaRepository for most projects.
  • Use pagination for large datasets.
  • Avoid returning huge result sets.
  • Use sorting to improve API usability.
  • Leverage batch operations when necessary.

Frequently Asked Questions (FAQ)

Can JpaRepository perform CRUD operations?

Yes. It inherits all CRUD methods from CrudRepository.


Can CrudRepository perform pagination?

No. Pagination requires PagingAndSortingRepository or JpaRepository.


Should new Spring Boot projects use JpaRepository?

Yes. JpaRepository is generally the preferred repository interface for modern applications.


Conclusion

CrudRepository, PagingAndSortingRepository and JpaRepository build upon each other. While CrudRepository provides basic CRUD operations and PagingAndSortingRepository adds pagination and sorting support, JpaRepository includes all features and additional JPA-specific functionality. For most Spring Boot applications, JpaRepository is the recommended choice.


Keywords

CrudRepository vs JpaRepository, JpaRepository vs PagingAndSortingRepository, Spring Data JPA Repository, JpaRepository Tutorial, CrudRepository Example, PagingAndSortingRepository Example, Spring Boot JPA, Spring Data Repository Comparison, Pagination Spring Data JPA, Sorting Spring Data JPA, JPA Interview Questions, Spring Boot Interview Questions, JpaRepository Methods, Spring Data JPA Tutorial, Java Backend Development



CrudRepository vs PagingAndSortingRepository vs JpaRepository in Spring Data JPA CrudRepository vs PagingAndSortingRepository vs JpaRepository in Spring Data JPA Reviewed by 13thGhost on 2:58 AM Rating: 5

No comments:

Recommended Posts

Powered by Blogger.