
Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:
Once the early-adopter seats are all used, the price will go up and stay at $33/year.
Last updated: April 3, 2025
In this quick article, we’ll focus on different kinds of Spring Data repository interfaces and their functionality. We’ll touch on:
Simply put, every repository in Spring Data extends the generic Repository interface, but beyond that, they each have different functionality.
Let’s start with the JpaRepository – which extends PagingAndSortingRepository and, in turn, the CrudRepository.
Each of these defines its functionality:
And so, because of this inheritance relationship, the JpaRepository contains the full API of CrudRepository and PagingAndSortingRepository.
When we don’t need the full functionality provided by JpaRepository and PagingAndSortingRepository, we can use the CrudRepository.
Let’s now look at a quick example to understand these APIs better.
We’ll start with a simple Product entity:
@Entity
public class Product {
@Id
private long id;
private String name;
// getters and setters
}
And let’s implement a simple operation – find a Product based on its name:
@Repository
public interface ProductRepository extends JpaRepository<Product, Long> {
Product findByName(String productName);
}
That’s all. The Spring Data Repository will auto-generate the implementation based on the name we provided it.
This was a very simple example, of course; you can go deeper into Spring Data JPA here.
Let’s now have a look at the code for the CrudRepository interface:
public interface CrudRepository<T, ID extends Serializable>
extends Repository<T, ID> {
<S extends T> S save(S entity);
T findOne(ID primaryKey);
Iterable<T> findAll();
Long count();
void delete(T entity);
boolean exists(ID primaryKey);
}
Notice the typical CRUD functionality:
This interface looks quite generic and simple, but actually, it provides all the basic query abstractions needed in an application.
Now, let’s have a look at another repository interface, which extends CrudRepository:
public interface PagingAndSortingRepository<T, ID extends Serializable>
extends CrudRepository<T, ID> {
Iterable<T> findAll(Sort sort);
Page<T> findAll(Pageable pageable);
}
This interface provides a method findAll(Pageable pageable), which is the key to implementing Pagination.
When using Pageable, we create a Pageable object with certain properties, and we’ve to specify at least the following:
So, let’s assume that we want to show the first page of a result set sorted by lastName, ascending, having no more than five records each. This is how we can achieve this using a PageRequest and a Sort definition:
Sort sort = new Sort(new Sort.Order(Direction.ASC, "lastName"));
Pageable pageable = new PageRequest(0, 5, sort);
Passing the pageable object to the Spring data query will return the results in question (the first parameter of PageRequest is zero-based).
Finally, we’ll have a look at the JpaRepository interface:
public interface JpaRepository<T, ID extends Serializable> extends
PagingAndSortingRepository<T, ID> {
List<T> findAll();
List<T> findAll(Sort sort);
List<T> save(Iterable<? extends T> entities);
void flush();
T saveAndFlush(T entity);
void deleteInBatch(Iterable<T> entities);
}
Again, let’s look at each of these methods in brief:
Clearly, the above interface extends PagingAndSortingRepository, which means it also has all methods present in the CrudRepository.
In the new version of Spring Data, the internals of some Repository classes have changed slightly, adding new functionalities and providing a simpler development experience.
We now have access to the advantageous List-based CRUD repository interface. Also, the class hierarchy of some spring-data Repository classes is based on a different structure.
All details are available in our New CRUD Repository Interfaces in Spring Data 3 article.
Beyond all the very useful advantages of these repositories, there are some basic downsides of directly depending on these as well:
This article covered some brief but important differences and features of Spring Data JPA repository interfaces. For more information, have a look at the series on Spring Persistence.