Databases Reference
In-Depth Information
Query generation from the method name
If we are building our queries by using the query generation from the method name
strategy, we can paginate query results by:
1.
Adding pagination support to the query method.
2.
Calling the query method from a service method.
Adding pagination support to the query method
Adding pagination support to our query method is rather simple. All we have to do
is make the following changes to the signature of the query method:
1.
Add the Pageable interface as a parameter of the query method.
2.
Determine the return type of the query method.
Since we are not interested in the page metadata, the signature of our query method
is given as follows:
public List<Contact> findByFirstNameStartingWithOrLastNameStartingWith
(String firstName, String lastName, Pageable page);
Modifying the service class
The modifications needed by the search() method of the
RepositoryContactService are rather simple. We get a reference to a
PageRequest object and pass it as a parameter to our query method. The
source code of the modified method is given as follows:
@Transactional(readOnly = true)
@Override
public List<Contact> search(SearchDTO dto) {
Pageable pageSpecification = buildPageSpecification(dto.
getPageIndex(), dto.getPageSize());
return repository.findByFirstNameStartingWithOrLastNameStartingWit
h(dto.getSearchTerm(), dto.getSearchTerm(), pageSpecification);
}
Named queries
If we want to paginate the query results of named queries, we have to:
1.
Add pagination support to the query method.
2.
Call the query method from a service method.
 
Search WWH ::




Custom Search