【问题标题】:I am looking for an alternative to Specifications for the given scenario我正在寻找给定场景的规格替代方案
【发布时间】:2018-11-22 13:42:26
【问题描述】:

我正在调用 JpaRepository 的findall() 方法如下

服务

on.findall(specificationsbuilder.getspecifications(params), paegable obj)  

specificationsbuilder.getspecifications(param)

返回规范

我的问题是,如果规范为空,我将 findall(specifications,paegable) 工作

【问题讨论】:

  • 我建议你试试看它是否有效。

标签: java spring hibernate spring-data-jpa jpa-criteria


【解决方案1】:

根据sourcecode of SimpleJpaRepository 它应该可以工作,因为@Nullable 说它会接受null:

@Override
    public Page<T> findAll(@Nullable Specification<T> spec, Pageable pageable) {

        TypedQuery<T> query = getQuery(spec, pageable);
        return isUnpaged(pageable) ? new PageImpl<T>(query.getResultList())
                : readPage(query, getDomainClass(), pageable, spec);
}

【讨论】:

    【解决方案2】:

    在这里阅读:https://jira.spring.io/browse/DATAJPA-121,截至最新的spring data jpa,如果您的参数为null,查询将自动形成null。

    另外,从 Spring data jpa 2.0 开始,spring 现在支持 @Nullable 注解。这有助于处理传递的空参数。

    @Nullable – 用于可以为空的参数或返回值。

    如果值为null,它会自动返回true,如果不是null,它会在表中搜索该值。

    【讨论】:

      猜你喜欢
      • 2012-06-17
      • 2011-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-29
      • 1970-01-01
      • 1970-01-01
      • 2018-10-05
      相关资源
      最近更新 更多