복붙노트

[SPRING] Spring 데이터 - 네이티브 쿼리로 페이징 할 수없는 이유

SPRING

Spring 데이터 - 네이티브 쿼리로 페이징 할 수없는 이유

MyEntity라는 엔티티가 있다고 가정 해 보겠습니다. @Query와 명명 된 쿼리를 사용하여 페이지 가능한 결과를 쿼리 할 수 ​​있습니다 (예 :

 @Query(value = "select e from MyEntity e where e.enabled = true")
 Page<MyEntity> findAllEnabled(Pageable pageable);

그러나 네이티브 쿼리에서도 동일한 결과를 얻을 수 없으므로

 @Query(value = "select * from my_entity where enabled = true", nativeQuery = true)
 Page<MyEntity> findAllEnabled(Pageable pageable);

작동하지 않습니다.

이것의 이유는 무엇입니까? 네이티브 쿼리로 Pageable 작업을 수행 할 수 있습니까?

해결법

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

    1.이것은 봄 데이터 jpa 문서 (http://docs.spring.io/spring-data/jpa/docs/1.8.0.M1/reference/html/)에서 제공되는 설명입니다.

    이것은 봄 데이터 jpa 문서 (http://docs.spring.io/spring-data/jpa/docs/1.8.0.M1/reference/html/)에서 제공되는 설명입니다.

    JPQL은 SQL 구현을 추상화하고 공급자의 특성을 추상화하고 올바른 SQL을 생성하기 위해 ORM 프레임 워크를 책임집니다.

  2. ==============================

    2.이것이 여전히 당신과 관련이 있는지 나는 모른다 : 최소한 봄 데이터 JPA 1.9.4에서는 두 개의 질의를 지정할 수있다.

    이것이 여전히 당신과 관련이 있는지 나는 모른다 : 최소한 봄 데이터 JPA 1.9.4에서는 두 개의 질의를 지정할 수있다.

    주어진 저장소 :

    interface FoobarEntityRepository extends JpaRepository<FoobarEntity, Integer> {
        Page findFoobarsSpecialQuery(String someParameter, final Pageable pageable);
    }
    

    엔티티에 네 개의 네이티브 쿼리를 추가 할 수 있습니다. 하나는 쿼리 자체에 대한 것이고 다른 하나는 count 문에 대한 것입니다.

    @Entity
    @SqlResultSetMappings({
        @SqlResultSetMapping(name = "SqlResultSetMapping.count", columns = @ColumnResult(name = "cnt"))
    })
    @NamedNativeQueries({
        @NamedNativeQuery(
                name = "FoobarEntity.findFoobarsSpecialQuery",
                resultClass = DailyPictureEntity.class,
                query = "Select * from foobars f where someValue = :someParameter "
        ),
        @NamedNativeQuery(
                name = "FoobarEntity.findFoobarsSpecialQuery.count",
                resultSetMapping = "SqlResultSetMapping.count",
                query = "Select count(*) as cnt from foobars f where someValue = :someParameter "
        )
    })
    FoobarEntity {
    }
    

    트릭은 카운트 쿼리를 .count 접미어로 지정하는 것입니다. 이것은 스프링 데이터 @Query 주석에도 적용됩니다.

    그러나 카운트 쿼리에 대한 SQL 결과 세트 매핑이 필요합니다.

    이것은 실제로 꽤 잘 작동합니다.

  3. ==============================

    3.Spring 데이터의 SpEL 용량과 함께 네이티브 쿼리와 함께 Pageable을 사용하는 방법이 있습니다.

    Spring 데이터의 SpEL 용량과 함께 네이티브 쿼리와 함께 Pageable을 사용하는 방법이 있습니다.

    이 저장소에서 예제를 찾을 수 있습니다.

    /**
         * @see DATAJPA-564
         */
        @Query(
                value = "select * from (select rownum() as RN, u.* from SD_User u) where RN between ?#{ #pageable.offset -1} and ?#{#pageable.offset + #pageable.pageSize}",
                countQuery = "select count(u.id) from SD_User u", nativeQuery = true)
        Page<User> findUsersInNativeQueryWithPagination(Pageable pageable);
    

    fromclause 또는 네이티브 쿼리에 하위 쿼리가 있고 동적 정렬을 적용하려는 경우 정렬 기능이 제대로 작동하지 않습니다. 이를 수행 할 수있는 방법은 where 절에서 하위 쿼리를 이동하는 것입니다. 페이지 데이터에 정렬 객체가 있으면 요청의 끝 부분에 스프링 데이터가 추가됩니다. (스프링 데이터 1.10.3 포함)

    더 좋은 방법은 가능한 경우 jpql에서 원시 쿼리를 변환하는 것입니다.

  4. from https://stackoverflow.com/questions/28529584/spring-data-why-its-not-possible-to-have-paging-with-native-query by cc-by-sa and MIT license