user6388353
user6388353

Reputation: 1957

Sorting with spring and querydsl

I'm trying to get sorting working in a @RepositoryRestResource where I'm creating a custom query a couple of querydsl interfaces but I seem to be missing something. The paging works but you can't sort on fields that have more than one word (shippedQty). Sorting on other fields works fine. Is this a PagingAndSortingRepository bug or do I have to do something else or multi-word fields?

@RepositoryRestResource(path = "/report", collectionResourceRel = "report", itemResourceRel = "report")
public interface ReportRepository extends PagingAndSortingRepository<Report, Long>, QueryDslPredicateExecutor<Report>,
        QuerydslBinderCustomizer<QReport> {

    @Override
    default void customize(QuerydslBindings bindings, QReport report) {
        bindings.including(
                report.description,
                report.item,
                report.program,
                report.shippedQty,
        );
        bindings.excludeUnlistedProperties(true);

        SingleValueBinding<NumberPath<Integer>, Integer> numberPathContains = (path, value) -> path.stringValue().contains(value.toString());

        bindings.bind(firstFill.description).first(StringPath::containsIgnoreCase);
        bindings.bind(firstFill.item).first(StringPath::containsIgnoreCase);
        bindings.bind(firstFill.program).first(StringPath::containsIgnoreCase);
        bindings.bind(firstFill.shippedQty).as("shipped_qty").first(numberPathContains);

 }
}

This sorts correctly:

http://localhost:8080/api/v1/report?page=0&size=5&sort=description,asc

This does not:

http://localhost:8080/api/v1/report?page=0&size=5&sort=shipped_qty,asc

Upvotes: 0

Views: 444

Answers (1)

valc
valc

Reputation: 111

I just ran into this problem myself. It turns out that Sort does not use the QueryDSL repository binding aliases, but instead uses the names of the "Q" entity pathes.

Upvotes: 1

Related Questions