Reputation: 12790
I have a design question:
Should I hard code the order by clause in the query statements?
public Page<Admin> search(String searchTerm, Pageable page) {
Pageable pageable = new PageRequest(page.getPageNumber(), page.getPageSize());
return adminRepository.search(searchTerm, pageable);
}
@Query("SELECT a FROM Admin a WHERE LOWER(a.firstname) LIKE LOWER(CONCAT('%', :searchTerm, '%')) OR LOWER(a.lastname) LIKE LOWER(CONCAT('%', :searchTerm, '%')) OR LOWER(a.email) LIKE LOWER(CONCAT('%', :searchTerm, '%')) OR LOWER(a.login) LIKE LOWER(CONCAT('%', :searchTerm, '%')) ORDER BY a.lastname ASC, a.firstname ASC")
public Page<Admin> search(@Param("searchTerm") String searchTerm, Pageable page);
Or, should I let the choice to the end user:
public Page<Admin> search(String searchTerm, Pageable page) {
return adminRepository.search(searchTerm, page);
}
@Query("SELECT a FROM Admin a WHERE LOWER(a.firstname) LIKE LOWER(CONCAT('%', :searchTerm, '%')) OR LOWER(a.lastname) LIKE LOWER(CONCAT('%', :searchTerm, '%')) OR LOWER(a.email) LIKE LOWER(CONCAT('%', :searchTerm, '%')) OR LOWER(a.login) LIKE LOWER(CONCAT('%', :searchTerm, '%'))")
public Page<Admin> search(@Param("searchTerm") String searchTerm, Pageable page);
Upvotes: 0
Views: 59
Reputation: 5067
This is a subjective question that is not usually welcome on SO. Anyhow, I would leave the things like ordering, sorting to the client. In this is way it is more robust. It might be the case that different users have different criteria for ordering.
Upvotes: 1