How do you specify multi-column OrderSpecifier for use in SpringData and QueryDsl? It's possible

So, I have the following query below:

public Iterable<Dealer> findAll(Dealer dealer) { QDealer qdealer = QDealer.dealer; BooleanExpression where = null; if(dealer.getId() != null && dealer.getId() != 0) { buildPredicate(qdealer.id.goe(dealer.getId())); } OrderSpecifier<String> sortOrder = QDealer.dealer.dealerCode.desc(); Iterable<Dealer> results = dlrRpstry.findAll(where, sortOrder); return results; } 

The above query is working fine. However, I would like to first sort the results by type dealerType, and then by the dealer code something like β€œorder by type of dealers asc, dealCode desc”. How to create an instance of OrderSpecifier so that the results are sorted by dealer type, and then by dealer code.

DealerRepository dlrRpstry extends JpaRepository, QueryDslPredicateExecutor

I use spring -data-jpa-1.1.0, spring -data-commons-dist-1.3.2 and querydsl-jpa-2.9.0.

If the OrderSpecifier cannot be configured to sort multiple columns, what would be an alternative solution that would satisfy my requirement to sort the results β€œby type of dealer asc, dealCode desc”.

Any help would be greatly appreciated. Thanks in advance.

Nick

+6
source share
1 answer

I do not have a JPA installation installation, but I believe that by reading the QueryDslJpaRepository documentation, you can simply do this:

 OrderSpecifier<String> sortOrder1 = QDealer.dealer.dealerType.asc(); OrderSpecifier<String> sortOrder2 = QDealer.dealer.dealerCode.desc(); Iterable<Dealer> results = dlrRpstry.findAll(where, sortOrder1, sortOrder2); 

Let us know if this works. Here is a link to a question / answer on stackoverflow that explains the syntax ... of the findAll () method:

fooobar.com/questions/3271 / ...

This means that you can have as many OrderSpecifiers as you want as additional parameters for the function.

+12
source

Source: https://habr.com/ru/post/958450/


All Articles