728x90
Springboot 에서 JPA 를 이용하여 CRUD 작업을 하는 플젝을 진행하던 중 오류가 발생하여 그 tracking 과정을 정리해보았다.
| 발생오류 로그
"message": "could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet"
| Issue Tracking
기본적으로 DB와 같은 구조를 가진 Entity 클래스의 Repository interface 를 생성하고 그 안에 JPA 쿼리문 작업을 진행한다.
public interface TestRepository extends JpaRepository<Entity명 , Long> {
JPARepository에서 두 테이블을 union 하려면 어쩔 수 없이 native query를 이용하여 구현해야 했다.
- 기본 JPA에서는 적용이 되지 않는 문제가 있었다.
네이티브 쿼리로 작성한 예시
@Query(nativeQuery = true,
value = "select * from (" +
"select a.id, a.name from test_table_a a left join test_table_b b on b.id = a.id where 조건절 union " +
"select c.id, c.name from test_table_c c left join test_table_d d on d.id = c.id where 조건절) e")
PageImpl<Entity명> findBySomething(LocalDateTime fromDate, LocalDateTime toDate, Pageable pageable);
위 쿼리문 실행시 아래와 같은 오류가 발생하며 페이징 처리가 되지 않음
"message": "could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet"
찾아보니, nativeQuery와 함께 페이징 처리를 하기 위해서는 countQuery를 추가해주어야 한다고 함
아래와 같이 수정
@Query(nativeQuery = true,
value = "select * from (" +
"select a.id, a.name from test_table_a a left join test_table_b b on b.id = a.id where 조건절 union " +
"select c.id, c.name from test_table_c c left join test_table_d d on d.id = c.id where 조건절) e",
countQuery = "select * from (" +
"select a.id, a.name from test_table_a a left join test_table_b b on b.id = a.id where 조건절 union " +
"select c.id, c.name from test_table_c c left join test_table_d d on d.id = c.id where 조건절) e")
PageImpl<Entity명> findBySomething(LocalDateTime fromDate, LocalDateTime toDate, Pageable pageable);
value 에서 작성한 nativeQuery와 동일하게 countQuery 값을 채워주어 해결되었다.
추가로 다른 작업 진행 중 비슷하지만 다른 오류가 발생한 케이스가 있어서 다음 게시글에서 정리하도록 하겠다.
반응형
'프로그래밍 언어 > JAVA' 카테고리의 다른 글
[JAVA] String -> LocalDate 날짜 형식 변환하여 두 날짜 간 일 수 차이 구하기(Period 이용) (0) | 2021.09.07 |
---|---|
[JAVA] List 내부에 포함된 null 항목 삭제하기 (0) | 2021.08.18 |
[Lombok] Spring Boot 생성자 어노테이션 비교 정리 (0) | 2021.07.07 |
[SpringBoot] 지정 시간에 배치 @Scheduled cron 속성 세팅 방법 (0) | 2021.06.29 |
[JAVA] SimpleDateFormat 이용하여 원하는 형식으로 날짜 출력하기 (0) | 2021.06.08 |