java 我可以在一个 Spring Data JPA 存储库方法上结合 @Query 定义和规范吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26379522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I combine a @Query definition and Specifications on one Spring Data JPA repository method?
提问by woytech
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
在一种存储库方法中同时使用注释和规范?例如,我想要一个这样的方法:
@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?
是否有可能或者我应该将整个查询构建为 aPredicate
并删除@Query
注释?
采纳答案by Vlad Mihalcea
First you might want to read this blog post. Second, according to the JpaSpecificationExecutor
interface that your repositories should implement you can run the following queries using Specifications:
首先,您可能想阅读这篇博文。其次,根据JpaSpecificationExecutor
您的存储库应实现的接口,您可以使用规范运行以下查询:
count(Specification<T> spec)
List<T> findAll(Specification<T> spec)
Page<T> findAll(Specification<T> spec, Pageable pageable)
List<T> findAll(Specification<T> spec, Sort sort)
T findOne(Specification<T> spec)
count(Specification<T> spec)
List<T> findAll(Specification<T> spec)
Page<T> findAll(Specification<T> spec, Pageable pageable)
List<T> findAll(Specification<T> spec, Sort sort)
T findOne(Specification<T> spec)
So you can't mix @Query
(or query-methods) and Specification
s.
所以你不能混合@Query
(或查询方法)和Specification
s。
You can express this condition:
你可以表达这个条件:
firstName <> ?1
using a Specification
instead. Then you can combine as many specifications as you want.
使用 aSpecification
代替。然后,您可以根据需要组合任意数量的规格。