Home » Java » How select distinct value with pageable Spring data JPA?

How select distinct value with pageable Spring data JPA?

Posted by: admin September 26, 2018 Leave a comment

Questions:

I want to make a distinct select in my table with pagination, but it is claiming this error. Does anyone know how to solve it?

org.postgresql.util.PSQLException: ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list

@Query(value = "SELECT DISTINCT budget.* FROM budget LEFT JOIN user_budget ON budget.id = user_budget.budget_id ORDER BY budget.created DESC, ?#{#pageable}", 
        countQuery = "SELECT DISTINCT count(*) FROM budget LEFT JOIN user_budget ON budget.id = user_budget.budget_id", 
        nativeQuery = true) 
public Page<Budget> findAllByfilter(Pageable pageable);
Answers: