简体   繁体   中英

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

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM