问题描述
是否可以在一个存储库方法中使用 @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
'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存储库方法中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!