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
Repository Hierarchy in Spring Data JPA
Basic Structure
CrudRepository
|
--> PagingAndSortingRepository
|
--> JpaRepository
|
--> ListCrudRepository
|
--> ListPagingAndSortingRepository
Overview of Interfaces
CrudRepository:
Provides CRUD operations: Create, Read, Update, Delete.
Returns
Optional
for nullable results.
ListCrudRepository:
An extension of
CrudRepository
.Returns
List
instead ofIterable
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()
andsaveAndFlush()
.
ListPagingAndSortingRepository:
Extends
ListCrudRepository
.Combines
ListCrudRepository
andPagingAndSortingRepository
.Returns
List
instead ofIterable
while supporting pagination and sorting.
CrudRepository
Key Features
Basic CRUD operations.
Methods:
Optional<T> findById(ID id); Iterable<T> findAll(); void deleteById(ID id); T save(T entity);
Example
@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
}
ListCrudRepository
Key Features
- Returns
List
instead ofIterable
, improving usability.
Example
@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
}
PagingAndSortingRepository
Key Features
Adds support for pagination and sorting.
Methods:
Iterable<T> findAll(Sort sort); Page<T> findAll(Pageable pageable);
Example
@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);
}
JpaRepository
Key Features
JPA-specific methods like
saveAndFlush
,flush
, anddeleteInBatch
.Suitable for complex queries and JPA-specific use cases.
Example
@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
}
ListPagingAndSortingRepository
Key Features
Combines
ListCrudRepository
withPagingAndSortingRepository
.Supports pagination, sorting, and returns
List
instead ofIterable
.
Example
@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();
}
Comparison Table
Repository Interface | CRUD Operations | Pagination/Sorting | JPA-Specific | Return Type for FindAll |
CrudRepository | ✅ | ❌ | ❌ | Iterable |
ListCrudRepository | ✅ | ❌ | ❌ | List |
PagingAndSortingRepository | ✅ | ✅ | ❌ | Iterable |
JpaRepository | ✅ | ✅ | ✅ | List |
ListPagingAndSortingRepository | ✅ | ✅ | ❌ | List |
When to Use Each Repository
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: