Can I combine a @Query definition and Specificatio

2019-02-12 05:08发布

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?

1条回答
冷血范
2楼-- · 2019-02-12 06:08

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.

查看更多
登录 后发表回答