【问题标题】:QuerydslBinderCustomizer not working in Spring Data JPA 2.0.7QuerydslBinderCustomizer 在 Spring Data JPA 2.0.7 中不起作用
【发布时间】:2018-12-09 04:52:05
【问题描述】:

我正在尝试使用 QuerydslBinderCustomizer 在我的 Rest 控制器中执行 @QuerydslPredicate 的使用。

我正在使用@Repositoy 实现来执行自定义查询并与代表查询访问级别的另一个表连接。

遵循文档

包含QuerydslBinderCustomizer的当前Spring JPA版本:spring-data-commons-2.0.7.RELEASE.jar

问题:

我正在尝试在serviceExecution.code 字段中应用like() 操作,但我只收到基于eq() 的谓词,并且根本没有调用customize 方法。

我也尝试将代码放在基于Repository 的接口中,但没有成功。

Repository 的实现是这样的:

@Repository 
public class ServiceExecutionQueryRepositoryImpl extends JpaQuerydslBaseRepository<Long, ServiceExecution> implements ServiceExecutionQueryRepository, QuerydslBinderCustomizer<QServiceExecution>, QuerydslPredicateExecutor<ServiceExecution> {

    @Override
    public void customize(QuerydslBindings bindings, QServiceExecution serviceExecution) {

        bindings.bind(serviceExecution.code).first((path, value) -> path.likeIgnoreCase(StringUtils.like(value)));
        // breakpoint never hit this method.
        // bindings is not applied to the query
        ... another bindings

    }
}

资源方法调用:

    @GetMapping("/service-executions")
    public ResponseEntity<Page<ServiceExecutionDTO>> getAllServiceExecutions(
        @RequestParam(required = false) MultiValueMap<String, String> parameters,
        @QuerydslPredicate(root = ServiceExecution.class) Predicate predicate, Pageable pageable) {
        Page<ServiceExecutionDTO> page = facade.findAll(parameters, predicate, pageable);
        return new ResponseEntity<>(page, HttpStatus.OK);
    }

生成的查询结果(在应用程序日志中可见)始终是这个(包括计数查询):

select o from ... where code = ?

谁能知道我可能遗漏了什么?与最近的 Spring Data JPA 版本有关吗?

详情:

这是一个使用Spring Boot的gradle项目,配置了apt。 除了这个问题,Querydsl 目前正在按预期工作。

可以检查每个方法上的Predicates 并转换为类似(我不知道是否/知道这可能),但即使有可能,这听起来也不是一个好的解决方法。

文档:https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#core.web.binding

我遵循的教程之一:https://spring.io/blog/2015/09/04/what-s-new-in-spring-data-release-gosling

类似问题:Spring @QuerydslPredicate Questions

QueryDsl web query on the key of a Map field(无效,因为它使用了以前版本的spring) 编辑

QuerydslBindingsFactory 似乎只加载绑定接口。

我正在使用带有@NoRepositoryBean 的接口,该接口未在搜索自定义绑定的地图中列出。这可能是 QuerydslBinderCustomizer 未被调用并添加到应用程序绑定的原因。

无论如何,我仍然不知道如何解决这个问题。

【问题讨论】:

    标签: java spring spring-boot spring-data-jpa querydsl


    【解决方案1】:

    看来您只是忘记将ServiceExecutionQueryRepositoryImpl 添加为@QuerydslPredicate 注释的bindings 参数:

    @GetMapping("/service-executions")
    public ResponseEntity<Page<ServiceExecutionDTO>> getAllServiceExecutions(
        @RequestParam(required = false) MultiValueMap<String, String> parameters,
        @QuerydslPredicate(root = ServiceExecution.class, bindings = ServiceExecutionQueryRepositoryImpl.class) Predicate predicate, 
        Pageable pageable
    ) {
        Page<ServiceExecutionDTO> page = facade.findAll(parameters, predicate, pageable);
        return new ResponseEntity<>(page, HttpStatus.OK);
    }
    

    参见示例:sb-querydsl-sd-demo

    【讨论】:

      猜你喜欢
      • 2017-09-21
      • 1970-01-01
      • 2018-03-20
      • 2015-01-20
      • 2017-06-09
      • 2016-03-04
      • 2021-07-27
      • 2018-04-03
      • 1970-01-01
      相关资源
      最近更新 更多