복붙노트

[SPRING] 스프링 데이터 jpa @query 및 페이지 가능

SPRING

스프링 데이터 jpa @query 및 페이지 가능

나는 Spring Data JPA를 사용하고 있으며 @Query를 사용하여 Pageable없이 쿼리를 정의하면 작동한다.

public interface UrnMappingRepository extends JpaRepository<UrnMapping, Long> {
    @Query(value = "select * from internal_uddi where urn like %?1% or contact like %?1%", 
           nativeQuery = true)
    List<UrnMapping> fullTextSearch(String text);
}

하지만 두 번째 매개 변수 인 Pageable을 추가하면 @Query가 작동하지 않고 Spring에서 메서드의 이름을 구문 분석 한 다음 예외를 throw합니다. No full full found. 이거 버그 야?

public interface UrnMappingRepository extends JpaRepository<UrnMapping, Long> {
    @Query(value = "select * from internal_uddi where urn like %?1% or contact like %?1%",
           nativeQuery = true)
    Page<UrnMapping> fullTextSearch(String text, Pageable pageable);
}

해결법

  1. ==============================

    1.비슷한 질문이 Spring 포럼에서 요청되었는데 페이지 매김을 적용하려면 두 번째 하위 쿼리를 유도해야한다는 지적이있었습니다. 하위 쿼리가 동일한 필드를 참조하기 때문에 쿼리가 참조하는 엔터티 / 테이블에 대한 별칭을 사용해야합니다. 이것은 당신이 쓴 곳을 의미합니다 :

    비슷한 질문이 Spring 포럼에서 요청되었는데 페이지 매김을 적용하려면 두 번째 하위 쿼리를 유도해야한다는 지적이있었습니다. 하위 쿼리가 동일한 필드를 참조하기 때문에 쿼리가 참조하는 엔터티 / 테이블에 대한 별칭을 사용해야합니다. 이것은 당신이 쓴 곳을 의미합니다 :

    select * from internal_uddi where urn like
    

    대신에 다음을 가져야합니다.

    select * from internal_uddi iu where iu.urn like ...
    
  2. ==============================

    2.기본 쿼리에서 페이지 매김을 사용할 수 있습니다. 여기에 문서가 있습니다 : https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#_native_queries

    기본 쿼리에서 페이지 매김을 사용할 수 있습니다. 여기에 문서가 있습니다 : https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#_native_queries

    그러나 카운트 쿼리를 직접 지정하여 페이지 매김에 대한 네이티브 쿼리를 사용할 수 있습니다. 예제 59. @Query "를 사용하여 쿼리 메서드에서 페이지 매김에 대한 네이티브 카운트 쿼리 선언"

    public interface UserRepository extends JpaRepository<User, Long> {
    
      @Query(value = "SELECT * FROM USERS WHERE LASTNAME = ?1",
        countQuery = "SELECT count(*) FROM USERS WHERE LASTNAME = ?1",
        nativeQuery = true)
      Page<User> findByLastname(String lastname, Pageable pageable);
    }
    
  3. ==============================

    3.UrnMapping 클래스가 internal_uddi 테이블에 매핑되어 있다고 가정하면 다음과 같이 제안합니다.

    UrnMapping 클래스가 internal_uddi 테이블에 매핑되어 있다고 가정하면 다음과 같이 제안합니다.

    @Repository
    public interface UrnMappingRepository extends JpaRepository<UrnMapping, Long> {
    
        @Query(value = "select iu from UrnMapping iu where iu.urn like %:text% or iu.contact like %:text%")
        Page<UrnMapping> fullTextSearch(@Param("text") String text, Pageable pageable);
    }
    

    동적 요청을 사용하여 원시 쿼리를 해제해야 할 수도 있습니다.

  4. ==============================

    4.쿼리를 다음으로 다시 작성하십시오.

    쿼리를 다음으로 다시 작성하십시오.

    select iu from internal_uddi iu where iu.urn.... 
    

    description : http://forum.spring.io/forum/spring-projects/data/126415-is-it-possible-to-use-query-and-pageable?p=611398#post611398

  5. from https://stackoverflow.com/questions/22345081/spring-data-jpa-query-and-pageable by cc-by-sa and MIT license