Taco Steemers

A personal blog.

Notes on Spring Data

Documentation

The main Spring Data project page is here . That page lists all sorts of integrations with other projects. The "learn" tab links to the API documentation and the reference manual

To find examples we have to search through their GitHub project listings. Basic examples can be found in the Spring Data Relational readme .

Pagination in Spring Data

We have to make a pagination-ready repository:

import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

@Repository
public interface MyDomainObjectRepository extends PagingAndSortingRepository<MyDomainObject, Long> {

    @Query("SELECT o FROM my_domain_object o WHERE o.createdAt >= ?1 AND o.createdAt < ?2")
    List<MyDomainObject> findByPeriod(final LocalDateTime from, final LocalDateTime to, Pageable pageable);
}

We can call this as:

List<MyDomainObject> results = myDomainObjectRepository.findByPeriod(from, to, PageRequest.of(pageNumber, pageSize));