硬编码订单条款?或者提供排序参数?

时间:2014-08-19 11:16:37

标签: rest jpa-2.0 jpql

我有一个设计问题:

我应该在查询语句中对 order by 子句进行硬编码吗?

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);

或者,我应该让最终用户选择:

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);

1 个答案:

答案 0 :(得分:1)

这是一个主观问题,在SO上通常不受欢迎。无论如何,我会将订购排序等内容留给客户端。在这方面它更健壮。可能是不同的用户有不同的订购标准。