Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you specify multi-column OrderSpecifier for use in SpringData and QueryDsl? Is this 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 query above works fine. However, I would like to sort the results by dealerType first, then by dealerCode somewhat like "order by dealerType asc, dealerCode desc". How do I instantiate the OrderSpecifier so that the results will be sorted by dealerType then by dealer code.

The DealerRepository dlrRpstry extends JpaRepository, QueryDslPredicateExecutor

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

If OrderSpecifier can not be configured to a multi-column sort order what would be the alternative solution that will satisfy my requirement of sorting the results "by dealerType asc, dealerCode desc".

Any help would be greatly appreciated. Thanks in advance.

Nick

like image 565
Dominique Morales Avatar asked Nov 20 '13 07:11

Dominique Morales


People also ask

How do you get data in descending order in spring boot?

One option is to use Spring Data's method derivation, whereby the query is generated from the method name and signature. All we need to do here to sort our data is include the keyword OrderBy in our method name, along with the property name(s) and direction (Asc or Desc) by which we want to sort.

What is Querydsl used for?

Querydsl is an extensive Java framework, which allows for the generation of type-safe queries in a syntax similar to SQL. It currently has a wide range of support for various backends through the use of separate modules including JPA, JDO, SQL, Java collections, RDF, Lucene, Hibernate Search, and MongoDB.

How do you write orderBy in JPA?

With JPA Criteria – the orderBy method is a “one stop” alternative to set all sorting parameters: both the order direction and the attributes to sort by can be set. Following is the method's API: orderBy(CriteriaBuilder. asc): Sorts in ascending order.

What is spring boot Querydsl?

Querydsl is a framework that enables the construction of statically typed SQL-like queries through its fluent API. Spring Data modules offer integration with Querydsl through QuerydslPredicateExecutor .


2 Answers

I do not have JPA install setup, but I believe 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 it works. Here is a link to a stackoverflow question/answer that explains the ... parameter syntax on the findAll() method:

https://stackoverflow.com/a/12994104/2879838

This means you can have as many OrderSpecifiers as you want as optional parameters to the function.

like image 121
David Fleeman Avatar answered Nov 04 '22 17:11

David Fleeman


If you don't want to create variable for each specifier, it can be done this way:

OrderSpecifier<?>[] sortOrder = new OrderSpecifier[] {
    QDealer.dealer.dealerType.asc(),
    QDealer.dealer.dealerCode.desc()
};
Iterable<Dealer> results = dlrRpstry.findAll(where, sortOrder); 
like image 28
horvoje Avatar answered Nov 04 '22 15:11

horvoje