QuerydslBinderCustomizer 在 Spring Data JPA 2.0.7 中不工作

QuerydslBinderCustomizer not working in Spring Data JPA 2.0.7

我正在尝试使用 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 版本有关吗?

详情:

这是一个 gradle 使用项目 Spring 启动,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

(无效,因为它使用了以前版本的spring) 编辑

似乎 QuerydslBindingsFactory 只加载绑定接口。

我正在使用 @NoRepositoryBean 的界面,该界面未列在搜索自定义绑定的地图中。也许这就是 QuerydslBinderCustomizer 没有被调用和添加到应用程序绑定的原因。

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

看来您只是忘记将 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