Databases Reference
In-Depth Information
Adding pagination support to the query method
We can add pagination support to a query method that is backed up by a named
query by adding the Pageable interface as a parameter of the query method. At this
point we do not need the page metadata for anything. Thus, the signature of our
query method is given as follows:
public List<Contact> findContacts(@Param("searchTerm") String
searchTerm, Pageable page);
Modifying the service class
Our implementation of the search() method of the RepositoryContactService
class is explained as follows:
1. We get the used like pattern.
2. We get the required PageRequest object.
3. We get the list of contacts by calling the modified query method.
The source code of our modified search() method is given as follows:
@Transactional(readOnly = true)
@Override
public List<Contact> search(SearchDTO dto) {
String likePattern = buildLikePattern(dto.getSearchTerm());
Pageable pageSpecification = buildPageSpecification(dto.
getPageIndex(), dto.getPageSize());
return repository.findContacts(likePattern, pageSpecification);
}
@Query annotation
We can paginate the query results of JPQL queries built with the @Query
annotation by:
1.
Adding the pagination support to a query method.
2.
Calling the query method from a service method.
 
Search WWH ::




Custom Search