我有一个简单的例子:查找具有优先级类别的所有项目>1和active=true。
Item有一个类别列表,每个类别都有一个名为priority的int字段。我正在尝试做一些类似的事情:
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);
但是我找不到合适的方法?有人能提供建议吗?
您可以像下面这样使用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 (List<Item>) itemRepository.findAll(booleanExpression);
}
这对我的4.3.1
版本有效,但对4.2.1
版本无效。请使用springboot检查此示例。