本文介绍了我可以将@Query定义和规范结合到一个Spring Data JPA存储库方法中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在一个存储库方法中使用 @Query 注释和规范?例如,我想要一个像这样的方法:

  @Query(value =SELECT e from EMPLOYEE where firstName< ;>?1)
public Page< Employee> findEmployeeBySomethigFancy(String firstName,Pageable pageable,Specification< Employee> emp);

是否可以或者应该将整个查询构建为 Predicate 并移除 @Query 注释?

解决方案

首先,您可能需要阅读此。其次,根据界面,您的存储库应该实现您可以使用规格运行以下查询:
$ b $ ul

  • count(规格说明)

  • 列表findAll(规范规范)
  • 页面findAll(规范规范,Pageable可分页)
  • 列表findAll规范规范,排序排序)

  • T findOne(规格说明)



  • 't混合@Query(或查询方法)和规格。

    您可以表达这种情况:

      firstName<> ?1 

    使用规范来代替。然后,您可以根据需要组合任意数量的规格。


    Is it possible to use both @Query annotation and specification in one repository method? For example I'd like to have a method like this:

    @Query(value="SELECT e from EMPLOYEE where firstName <> ?1")
    public Page<Employee> findEmployeeBySomethigFancy(String firstName, Pageable pageable, Specification<Employee> emp);
    

    Is it possible or should I build the whole query as a Predicate and remove the @Query annotation?

    解决方案

    First you might want to read this blog post first. Second, according to the JpaSpecificationExecutor interface that your repositories should implement you can run the following queries using Specifications:

    • count(Specification spec)
    • List findAll(Specification spec)
    • Page findAll(Specification spec, Pageable pageable)
    • List findAll(Specification spec, Sort sort)
    • T findOne(Specification spec)

    So you need can't mix @Query (or query-methods) and Specifications.

    You can express this condition:

    firstName <> ?1
    

    using a Specification instead. Then you can combine as many specifications as you want.

    这篇关于我可以将@Query定义和规范结合到一个Spring Data JPA存储库方法中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    10-22 21:29