英文:
Specification with Spring JPA not applying the clauses mentioned
问题
我有一个根据筛选条件构建动态查询的要求。我有一个包含筛选条件的数组,基于数组中的条目,我正在构建Predicate(谓词)。但是根据我的当前实现,似乎规范(spec)没有传递到findall查询中。我得到的响应包含了所有的数据。如果有什么遗漏或错误的地方,有人能纠正我吗?
我的仓库接口
```java
@Repository
public interface Company extends JpaRepository<Comp, Integer>{
List<Comp> findAll(Specification spec);
}
我的规范构建器
private Specification specificationOnFilter(List<Filter> filters) {
new Specification<Comp>() {
@Override
Predicate toPredicate(Root<Comp> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
List<Predicate> predicates = new ArrayList<>();
// 构建谓词,添加到predicates对象中
return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
}
};
}
我的最终调用如下
MyRepository.findAll(specificationOnFilter(filter));
<details>
<summary>英文:</summary>
I have a requirement for building a dynamic query based on filter. I have an Array which contain the filter criteria based on the entries in the Array I am building the Predicate. But with my current implementation its like the spec is not getting passed to the findall query. The response I am getting has all the data. Could someone correct me if something is missing or wrong.
My Repository interface
```java
@Repository
public interface Company extends JpaRepository<Comp, Integer>{
List<Comp> findAll(Specification spec);
}
My Spec builder
private Specification specificationOnFilter(List<Filter> filters) {
new Specification<Comp>() {
@Override
Predicate toPredicate(Root<Comp> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
List<Predicate> predicates = new ArrayList<>();
// Building predicate, adding into the predicates object
return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
}
};
}
My final call is like below
MyRepository.findAll(specificationOnFilter(filter));
答案1
得分: 0
这行代码:
List<Predicate> predicates = new ArrayList<>(); // 构建谓词并添加到谓词对象中
return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
似乎只是从一个空的 Predicate 列表生成谓词。因此,最终的谓词将传递所有数据,因为它没有包含从零谓词构建的信息。
predicates.toArray(new Predicate[predicates.size()]) // 在您的情况下将为空。
您应该使用您方法的参数中的 List<Filter> filter
,而不仅仅是创建一个空的 new ArrayList<>()
,然后从中构建 List<Predicate>
。
此外,在您的问题中,您没有提供您正在使用的任何库的信息。Filter
、Specification
和 Scope
类是什么(我猜这是来自 Spring Cloud)。因此,很难重现问题,您将获得抽象的答案。
英文:
This line of code:
List<Predicate> predicates = new ArrayList<>(); // Building predicate, adding into the predicates object
return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
Seem's to just generate predicate from empty ArrayList of predicate's. Therefore your final predicate will pass all the data since it doesn't contain it was builded from zero predicates.
predicates.toArray(new Predicate[predicates.size()]) // will be empty in your case.
You should involve List<Filter> filter
from argument of your method and build predicates List<Predicate>
from it instead of just creating new ArrayList<>()
which is empty.
Also in your question you don't provide any information on what libraries you are using. What are Filter
and Specification
and Scope
classes (this comes from spring cloud i guess). Therefore it's hard to reproduce issue and you will be provided with abstract answer's.
专注分享java语言的经验与见解,让所有开发者获益!
评论