代码之家  ›  专栏  ›  技术社区  ›  Deividi Cavarzan

QueryDslBinderCustomizer在Spring Data JPA 2.0.7中不工作

  •  2
  • Deividi Cavarzan  · 技术社区  · 8 年前

    我想用 QuerydslBinderCustomizer 使用 @QuerydslPredicate 在我的休息控制器里。

    我用的是 @Repositoy 实现以执行自定义查询并与表示查询访问级别的其他表联接。

    遵循文档

    当前的spring jpa版本包含 查询绑定自定义程序 以下内容: spring-data-commons-2.0.7.RELEASE.jar

    问题是:

    我想申请一个 like() 中的操作 serviceExecution.code 字段,但我只接收基于 eq() 以及 customize 根本不调用方法。

    我还尝试将代码放在基于接口的 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 在每种方法上,并转换为like(我不知道它是否可能),但即使是可能的,听起来也不是一个好的解决方法。

    文件: 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 ,在搜索自定义绑定的映射中未列出。也许这就是 查询绑定自定义程序 没有被调用并添加到应用程序绑定中。

    不管怎样,我还是不知道该怎么解决。

    1 回复  |  直到 8 年前
        1
  •  1
  •   Cepr0    8 年前

    你好像忘了加上 ServiceExecutionQueryRepositoryImpl 作为 bindings 参数 @QuerydslPredicate 注释:

    @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