英文:
QueryDsl BooleanBuilder: How to create a predicate that compares contents of list?
问题
我有一个简单的例子:查找所有具有优先级大于1且活动状态为true的项目。
项目有一个类别列表,每个类别都有一个名为优先级的整数字段。
我正在尝试做类似以下的事情:
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);
但我找不到正确的方法要怎么用?请问有人可以建议吗?
英文:
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
得分: 1
你可以像下面这样使用 BooleanExpression
-
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 itemRepository.findAll(booleanExpression);
}
这个在版本 4.3.1
下对我有效,但在 4.2.1
下无效。请查看这个使用Spring Boot的示例。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论