1. Overview

In this tutorial, we’ll look at the new repository interfaces introduced in Spring Data 3.

Spring Data 3 introduced List-based CRUD repository interfaces, which can be used to replace existing CRUD repository interfaces that return Iterable. In addition, the paging and sorting interfaces don’t inherit from original CRUD repositories by default and instead leave that option to the user. We’ll see how these interfaces differ from the existing interfaces and how to use them.

2. Project Setup

Let’s start by setting up our project. We’ll create a Spring Boot application containing a simple entity and repositories that will use the new interfaces.

2.1. Dependencies

Let’s start by adding the required dependencies to our project. We’ll add the Spring Boot Starter Data dependency:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
    <version>3.1.5</version>
</dependency>

In addition to this, we’ll configure the database. Any SQL database can be used. We’ll use the in-memory H2 database for this tutorial. Let’s add a dependency for the same:

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
</dependency>

2.2. Entity

Let’s create an entity Book that we’ll use in our repositories:

@Entity
public class Book {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String title;
    private String author;
    private String isbn;

    // Constructor, getters and setters
}

Now that we have our entity, let’s look at how to use the new interfaces to interact with the database.

3. List-based CRUD Repositories

Spring Data 3 introduces a new set of CRUD repository interfaces that return a list of entities. These interfaces are similar to the existing interfaces, except they return a List instead of an Iterable. This enables us to use advanced methods from the List interface, such as get() and indexOf().

3.1. List-based Repository Interfaces

Three new interfaces have been added to Spring Data 3.

The ListCrudRepository interface provides methods for basic CRUD operations such as save()delete(), and findById(). The key difference between ListCrudRepository and its Iterable-based counterpart is that the returned values are now lists, which allows us to perform more advanced operations on the returned data.

The ListQuerydslPredicateExecutor interface provides methods for executing queries using Querydsl predicates. Querydsl is a framework that allows us to build type-safe SQL-like queries in Java. With ListQuerydslPredicateExecutor, we can execute Querydsl queries and return the results as a list.

The ListQueryByExampleExecutor interface provides methods for executing queries using example entities and returns the results as a list. An example entity is an entity that contains the values that we want to use to search for other entities. For example, if we have a Book entity with the title Spring Data, we can create an example entity with the title Spring Data and use it to search for other books with the same title.

3.2. ListCrudRepository

Let’s look at the ListCrudRepository interface in detail. We’ll create a repository that will use this interface to interact with the database:

@Repository
public interface BookListRepository extends ListCrudRepository<Book, Long> {
    
    List<Book> findBooksByAuthor(String author);
}

The above repository extends the ListCrudRepository interface, which provides us with the basic CRUD methods. We can use the existing repository methods to save, delete, and find books in the database.

In addition to these methods, we defined the findBooksByAuthor() method to find books by their author.

3.3. Using List-based Repository Interfaces

Let’s look at how we can use this repository to interact with the database:

@SpringBootTest
public class BookListRepositoryIntegrationTest {

    @Autowired
    private BookListRepository bookListRepository;
    
    @Test
    public void givenDbContainsBooks_whenFindBooksByAuthor_thenReturnBooksByAuthor() {
        Book book1 = new Book("Spring Data", "John Doe", "1234567890");
        Book book2 = new Book("Spring Data 2", "John Doe", "1234567891");
        Book book3 = new Book("Spring Data 3", "John Doe", "1234567892");
        bookListRepository.saveAll(Arrays.asList(book1, book2, book3));

        List<Book> books = bookListRepository.findBooksByAuthor("John Doe");
        assertEquals(3, books.size());
    }
}

We first created three books and saved them to the database. Then, we used the findBooksByAuthor() method to find all the books by the author John Doe. At last, we verified that the returned list contains the three books that we created.

Please note that we called the size() method on the returned list. This wouldn’t have been possible if the repository had used the Iterable-based interfaces because the Iterable interface doesn’t provide a size() method.

4. Sorting Repositories

To be able to use the new List-based interfaces, Spring Data 3 had to make changes to the existing sorting interfaces. The sorting repositories don’t extend the older crud repositories anymore. Instead, the user has the option to extend the new List-based interfaces or the Iterable-based interfaces along with the sorting interfaces. Let’s look at how we can use the latest sorting interfaces.

4.1. PagingAndSortingRepository

Let’s look at the PagingAndSortingRepository interface in detail. We’ll create a repository that will use this interface to interact with the database:

@Repository
public interface BookPagingAndSortingRepository extends PagingAndSortingRepository<Book, Long>, ListCrudRepository<Book, Long> {
    
    List<Book> findBooksByAuthor(String author, Pageable pageable);
}

The above repository extends the ListCrudRepository interface, which provides us with the basic CRUD methods. In addition to this, we extended the PagingAndSortingRepository interface, which provides us with methods for sorting and pagination.

In older versions of Spring Data, we didn’t need to extend a CRUD repository interface explicitly. Only the sorting and pagination interfaces were enough. We’ve defined a new method called findBooksByAuthor() that takes a Pageable parameter and returns a list of books. In the next section, we’ll look at how we can use this method to sort and paginate the results.

4.2. Using Sorting Repository Interfaces

Let’s look at how we can use this repository to interact with the database:

@SpringBootTest
public class BookPagingAndSortingRepositoryIntegrationTest {

    @Autowired
    private BookPagingAndSortingRepository bookPagingAndSortingRepository;
    
    @Test
    public void givenDbContainsBooks_whenfindBooksByAuthor_thenReturnBooksByAuthor() {
        Book book1 = new Book("Spring Data", "John Doe", "1234567890");
        Book book2 = new Book("Spring Data 2", "John Doe", "1234567891");
        Book book3 = new Book("Spring Data 3", "John Doe", "1234567892");
        bookPagingAndSortingRepository.saveAll(Arrays.asList(book1, book2, book3));

        Pageable pageable = PageRequest.of(0, 2, Sort.by("title").descending());
        List<Book> books = bookPagingAndSortingRepository.findBooksByAuthor("John Doe", pageable);
        assertEquals(2, books.size());
        assertEquals(book3.getId(), books.get(0).getId());
        assertEquals(book2.getId(), books.get(1).getId());
    }
}

Just like before, we first created three books and saved them to the database. Then, we used the findBooksByAuthor() method to find all the books by the author John Doe. But this time, we passed a Pageable object to the method to sort the results by the title in descending order and to return only the first two results.

At last, we verified that the returned list contains the two books that we created. We also verified that the books are sorted by the title in descending order so that the book with the title Spring Data 3 is returned first and the book with the title Spring Data 2 is returned second.

4.3. Other Sorting Interfaces

In addition to the PagingAndSortingRepository interface, the below interfaces have also been changed:

  • ReactiveSortingRepository no longer inherits from ReactiveCrudRepository
  • CoroutineSortingRepository no longer inherits from CoroutineCrudRepository
  • RxJavaSortingRepository no longer inherits from RxJavaCrudRepository

5. Conclusion

In this article, we explored the new List-based interfaces that have been added to Spring Data 3. We looked at how to use these interfaces to interact with the database. We also looked at the changes that have been made to the existing sorting interfaces to be able to use the new List-based interfaces along with them.

The code examples for this article can be found over on GitHub.

Course – LSD (cat=Persistence)

Get started with Spring Data JPA through the reference Learn Spring Data JPA course:

>> CHECK OUT THE COURSE
res – Persistence (eBook) (cat=Persistence)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.