Search code examples
quarkus-panache

How can I filter a PanacheQuery by specific fields?


I would like to sort, filter and page a PanacheQuery:

    @GET
    public Response listAllPaged(@BeanParam PagedRequest req) {

        // Sorting
        PanacheQuery<MyEntity> all = MyEntity.findAll(Sort.by(req.sortBy, Sort.Direction.Ascending));
        
        // Filtering
        [...] ? I want to keep only results where req.filterBy == req.filterValue
        
        // Paging
        List<MyDTO> list = all
            .page(Page.of(pagedRequest.pageNum, pagedRequest.pageSize))
            .stream()
            .map(myMapper::toDto)
            .collect(Collectors.toList());
            return Response.ok().entity(new PagedResponse<>(all.count(), list)).build();
    }

PanacheQuery has a .filter() Method, but as far as I understood, this is for predefined Hibernate Filters.

I would like to filter my results with field-value pairs, like this:

all.filter("status", "alive")
all.filter("age", "25")

I could not find many examples on how to achieve this, and the Quarkus Panache Docs are rather quiet on the subject. Any suggestions?


Solution

  • Since Panache does not support JPA Criteria / Predicates, I ended up building the query myself, like here.