Understanding Repositories in Spring Data JPA!

Learner, Love to make things simple, Full Stack Developer, StackOverflower, Passionate about using machine learning, deep learning and AI
Search for a command to run...

Learner, Love to make things simple, Full Stack Developer, StackOverflower, Passionate about using machine learning, deep learning and AI
No comments yet. Be the first to comment.
Spring Boot tutorials, java and spring cloud tutorials where we can cover other parts like microservices, etc.
Introduction In modern applications, ensuring that scheduled tasks do not run concurrently across distributed systems is crucial for maintaining data integrity and consistency. One common solution for this problem is using distributed locks. In this ...
Move beyond traditional RESTful thinking. Learn how to design APIs specifically for MCP (Model Context Protocol) servers. This guide covers the shift in mindset, a practical OpenAPI 3.1 example, and a Spring Boot implementation to make your services ...

Extending Kestra to Every Corner of Your Data Stack. Introduction: The Power of Plugins Imagine you're a master chef. You don't just have one knife - you have specialized tools for every task: a paring knife for delicate work, a chef's knife for chop...
Mastering Complex Orchestration Scenarios. Introduction: The Orchestrator's Toolkit Imagine you're conducting a symphony. You don't just wave your baton - you cue sections, adjust tempo, handle surprises, and ensure harmony. That's what advanced work...
From Data Extraction to Loading - A Practical Guide Introduction: Why ETL Still Matters in the Modern Data Stack Remember when data engineering was "extract, transform, load"? Some say ETL is dead, replaced by ELT, reverse ETL, and data mesh. But her...
Building Blocks of Declarative Orchestration. Introduction: The Power of Simplicity Imagine trying to build a house without understanding bricks, beams, and blueprints. That's what using an orchestration tool without understanding its core concepts f...
Spring Data JPA provides several repository interfaces to facilitate data persistence and retrieval. These interfaces extend each other to build upon functionality, starting from basic CRUD operations to more advanced capabilities like pagination and sorting.
This article explores the following repository interfaces:
CrudRepository
ListCrudRepository
JpaRepository
PagingAndSortingRepository
ListPagingAndSortingRepository
CrudRepository
|
--> PagingAndSortingRepository
|
--> JpaRepository
|
--> ListCrudRepository
|
--> ListPagingAndSortingRepository
CrudRepository:
Provides CRUD operations: Create, Read, Update, Delete.
Returns Optional for nullable results.
ListCrudRepository:
An extension of CrudRepository.
Returns List instead of Iterable for find operations, making it easier to work with collections.
PagingAndSortingRepository:
Extends CrudRepository.
Adds support for pagination and sorting.
JpaRepository:
Extends PagingAndSortingRepository.
Provides JPA-specific methods like flush() and saveAndFlush().
ListPagingAndSortingRepository:
Extends ListCrudRepository.
Combines ListCrudRepository and PagingAndSortingRepository.
Returns List instead of Iterable while supporting pagination and sorting.
Basic CRUD operations.
Methods:
Optional<T> findById(ID id);
Iterable<T> findAll();
void deleteById(ID id);
T save(T entity);
@Repository
public interface EmployeeCrudRepository extends CrudRepository<Employee, Long> {}
// Usage
@Autowired
private EmployeeCrudRepository repository;
public void demoCrud() {
Employee emp = new Employee("John", "Developer");
repository.save(emp); // Create
Optional<Employee> retrievedEmp = repository.findById(emp.getId()); // Read
repository.deleteById(emp.getId()); // Delete
}
List instead of Iterable, improving usability.@Repository
public interface EmployeeListCrudRepository extends ListCrudRepository<Employee, Long> {}
// Usage
@Autowired
private EmployeeListCrudRepository repository;
public void demoListCrud() {
List<Employee> employees = repository.findAll(); // Returns a List instead of Iterable
}
Adds support for pagination and sorting.
Methods:
Iterable<T> findAll(Sort sort);
Page<T> findAll(Pageable pageable);
@Repository
public interface EmployeePagingRepository extends PagingAndSortingRepository<Employee, Long> {}
// Usage
@Autowired
private EmployeePagingRepository repository;
public void demoPagingAndSorting() {
Pageable pageable = PageRequest.of(0, 5, Sort.by("name").ascending());
Page<Employee> page = repository.findAll(pageable);
}
JPA-specific methods like saveAndFlush, flush, and deleteInBatch.
Suitable for complex queries and JPA-specific use cases.
@Repository
public interface EmployeeJpaRepository extends JpaRepository<Employee, Long> {}
// Usage
@Autowired
private EmployeeJpaRepository repository;
public void demoJpa() {
repository.saveAndFlush(new Employee("Jane", "Manager")); // Saves and flushes immediately
repository.deleteAllInBatch(); // Efficient batch delete
}
Combines ListCrudRepository with PagingAndSortingRepository.
Supports pagination, sorting, and returns List instead of Iterable.
@Repository
public interface EmployeeListPagingRepository extends ListPagingAndSortingRepository<Employee, Long> {}
// Usage
@Autowired
private EmployeeListPagingRepository repository;
public void demoListPagingAndSorting() {
Pageable pageable = PageRequest.of(0, 5);
List<Employee> employees = repository.findAll(pageable).getContent();
}
| Repository Interface | CRUD Operations | Pagination/Sorting | JPA-Specific | Return Type for FindAll |
| CrudRepository | ✅ | ❌ | ❌ | Iterable |
| ListCrudRepository | ✅ | ❌ | ❌ | List |
| PagingAndSortingRepository | ✅ | ✅ | ❌ | Iterable |
| JpaRepository | ✅ | ✅ | ✅ | List |
| ListPagingAndSortingRepository | ✅ | ✅ | ❌ | List |
CrudRepository: For simple CRUD operations with minimal dependencies.
ListCrudRepository: When working with collections and List is preferred.
PagingAndSortingRepository: When pagination or sorting is required.
JpaRepository: For advanced JPA-specific functionalities.
ListPagingAndSortingRepository: When combining ListCrudRepository with pagination/sorting.
More such articles: