首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >QuerydslBinderCustomizer在SpringDataJPA2.0.7中不工作

QuerydslBinderCustomizer在SpringDataJPA2.0.7中不工作
EN

Stack Overflow用户
提问于 2018-06-30 01:56:08
回答 1查看 3.1K关注 0票数 2

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

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

在文件之后

包含QuerydslBinderCustomizerspring-data-commons-2.0.7.RELEASE.jar的当前Spring版本

问题是:

我试图在like()字段中应用serviceExecution.code操作,但是我只接收基于eq()的谓词,并且根本不调用customize方法。

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

Repository实现是这样的:

代码语言:javascript
复制
@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

    }
}

资源方法调用:

代码语言:javascript
复制
    @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版本有关?

详细信息:

它是一个使用Spring Boot的项目,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问题

地图字段键上的QueryDsl web查询 (无效,因为它使用了以前版本的spring) 编辑

QuerydslBindingsFactory似乎只为绑定加载接口。

我使用的是@NoRepositoryBean接口,它不在搜索自定义绑定的映射中列出。这可能是QuerydslBinderCustomizer没有被调用并添加到应用程序绑定中的原因。

不管怎样,我还是不知道怎么解决这个问题。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-30 09:55:46

似乎您只是忘记添加ServiceExecutionQueryRepositoryImpl作为@QuerydslPredicate注释的bindings参数:

代码语言:javascript
复制
@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-演示

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51111121

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档