桃桃桃子
桃桃桃子

Reputation: 189

Spring data JPA nativeQuery order by is invalid

The Spring Data Jpa Method like this:

@Query("select pb.id,pp.max_borrow_amt,pp.min_borrow_amt
from product_loan_basic pb left join product_loan_price pp on pb.code=pp.product_code 
 where pb.code IN(?1) and pb.status='publish' order by  ?2 ",
nativeQuery = true)  
List<Object[]> findByCodesIn(List<String> codes,String orderby);

then order by is " max_borrow_amt desc ", but this is invalid.
the List is disordered.

Upvotes: 12

Views: 17625

Answers (2)

桃桃桃子
桃桃桃子

Reputation: 189

Thanks everyone! My problem has been solved.

If you want to use Spring data jpa nativeQuery & Sort, you should do like this:

 @Query(
          value ="select pb.id,pp.max_borrow_amt from product_loan_basic pb left join product_loan_price pp on pb.code=pp.product_code ORDER BY ?#{#pageable}  ",
          countQuery = "select count(*) from product_loan_basic",
          nativeQuery = true
  )
  Page<Object[]> findAllProductsAndOrderByAndSort(Pageable pageable);

?#{#pageable} is required and countQuery is required.

Pageable pageable = new PageRequest(0,1000,Sort.Direction.DESC,"id");

then the result is sorted.

See Spring Data and Native Query with pagination.

Upvotes: 1

Cepr0
Cepr0

Reputation: 30289

Dynamic sorting in Spring Data JPA

If you used a JPA query you could use Sort as an argument of your query method to define the sorting order:

@Query("select m from Model m")
List<Model> getSortedList(Sort sort);

and then, for example:

List<Model> models = getSortedList(Sort.by(Sort.Direction.DESC, "name"));

But Spring Data JPA can't use Sort with native queries:

Spring Data JPA does not currently support dynamic sorting for native queries, because it would have to manipulate the actual query declared, which it cannot do reliably for native SQL.

However you can use Pageable and its implementation PageRequest instead:

@Query(value = "select m.name as name from models m", nativeQuery = true)
List<ModelProjection> getSortedList(Pageable p);

and then:

List<ModelProjection> modelNames = getSortedList(PageRequest.of(0, 1000, Sort.Direction.DESC, "name"));

P.S. Instead of array of Objects as returned parameters, it's better to use projections, for example:

public interface ModelProjection {
    String getName();
}

Note that in this case the good practice is to use aliases in queries (ie m.name as name). They must match with correspondent getters in the projection.

Working demo and test.

Upvotes: 16

Related Questions