Can I combine a @Query definition with a Specification in a Spring Data JPA repository method?

前端 未结 1 977
不思量自难忘°
不思量自难忘° 2020-12-03 10:17

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:

@Quer         


        
相关标签:
1条回答
  • First thing, you should read this Spring blog post.

    Second, according to the JpaSpecificationExecutor interface, that your repositories should implement, the following method take a Specification argument:

    • 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 a @Query definition with a Specification.

    However, since you can express the firstName <> ?1 condition using a Specification and because you combine as many specifications as you want, you can rewrite the whole @Query using Specification(s) only.

    0 讨论(0)
提交回复
热议问题