QueryDsl BooleanBuilder: How to create a predicate that compares contents of list?

Viewed 593

I have simple example: to find all items that has a category with priority > 1 and active = true .

Item has a list of categories, each has an int field called priority. I'm trying to do something like:

builder = new BooleanBuilder();
Predicate predicate = builder.and(item.categories.any(category.priority.goe(1).and(category.active.eq(true))));
Iterable<Item> iterable = itemRepository.findAll(predicate);

but i cannot find the right method to use? Can someone pls advise?

1 Answers

You can use BooleanExpression like below -

public List<Item> getItems() {
        QItem item = QItem.item;
        QCategory category = QCategory.category;
        BooleanExpression booleanExpression = item.categories.contains(
                JPAExpressions.selectFrom(category).
                  where(category.item.eq(item).
                   and(category.priority.eq(1000)
                           .and(category.active.eq(true)))));
        return (List<Item>) itemRepository.findAll(booleanExpression);
    }

This worked for me with version 4.3.1 but not with 4.2.1. Please check this example using springboot.

Related