使用Spring JPA的规范未应用所提及的子句。

huangapple 未分类评论44阅读模式
英文:

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&lt;Comp, Integer&gt;{
    List&lt;Comp&gt; findAll(Specification spec);
}

My Spec builder

private Specification specificationOnFilter(List&lt;Filter&gt; filters) {
    new Specification&lt;Comp&gt;() {
        @Override
        Predicate toPredicate(Root&lt;Comp&gt; root, CriteriaQuery&lt;?&gt; query, CriteriaBuilder criteriaBuilder) {
            List&lt;Predicate&gt; predicates = new ArrayList&lt;&gt;();
            // 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>

此外,在您的问题中,您没有提供您正在使用的任何库的信息。FilterSpecificationScope 类是什么(我猜这是来自 Spring Cloud)。因此,很难重现问题,您将获得抽象的答案。

英文:

This line of code:

List&lt;Predicate&gt; predicates = new ArrayList&lt;&gt;(); // 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&lt;Filter&gt; filter from argument of your method and build predicates List&lt;Predicate&gt; from it instead of just creating new ArrayList&lt;&gt;() 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.

huangapple
  • 本文由 发表于 2020年7月24日 00:33:58
  • 转载请务必保留本文链接:https://java.coder-hub.com/63059012.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定