ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java – QuerydslBinderCustomizer无法在Spring Data JPA 2.0.7中使用

2019-09-10 23:19:41  阅读:1254  来源: 互联网

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


我正在尝试使用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

    }
}

Resource方法调用:

    @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);
    }

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

从…中选择o代码=?

谁能知道我可能错过什么?是否与最近的Spring Data JPA版本有关?

细节:

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

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

文件: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未被调用并添加到应用程序绑定的原因.

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

解决方法:

您似乎忘了将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

标签:querydsl,java,spring,spring-boot,spring-data-jpa
来源: https://codeday.me/bug/20190910/1801958.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有