Quantcast
Channel: Spring Community Forums - Data
Viewing all articles
Browse latest Browse all 297

Potential conflict between sorting in the derivation method name and a Page param ?

$
0
0
Hello,

I have a query derivation method with the name:

public Page<Admin> findByFirstnameStartingWithOrLastnameStartingWithO rderByLastnameAscFirstnameAsc(String searchTerm, Pageable page);

The name contains a sorting clause with its: OrderByLastnameAscFirstnameAsc(

What happens if I use it like this ?
Code:

// Should a Sort order be passed in the page request ? What of the sort order in the method name ?
Pageable pageRequest = buildPageRequest(0, 10);
Page<Admin> admins = adminRepository.findByFirstnameStartingWithOrLastnameStartingWithOrderByLastnameAscFirstnameAsc("zfirstname", pageRequest);

private Pageable buildPageRequest(int pageIndex, int pageSize) {
        Sort sort = new Sort(new Sort.Order(Sort.Direction.ASC, "lastname"), new Sort.Order(Sort.Direction.ASC, "firstname"));
        return new PageRequest(pageIndex, pageSize, sort);
}

In the same fashion, if the query is done with a @Query annotation, do we have an issue as well ?
Code:

@Query("SELECT a FROM Admin a WHERE LOWER(a.firstname) LIKE LOWER(CONCAT('%', :searchTerm)) OR LOWER(a.lastname) LIKE LOWER(CONCAT('%', :searchTerm)) ORDER BY a.lastname ASC, a.firstname ASC")
public Page<Admin> findByFirstnameStartingWithOrLastnameStartingWith(@Param("searchTerm") String searchTerm, Pageable page);

Kind Regards,

Viewing all articles
Browse latest Browse all 297

Trending Articles