du.study기록공간

spring + h2환경에서 JPA native query를 사용했을때 맞이한 문제점 본문

스프링

spring + h2환경에서 JPA native query를 사용했을때 맞이한 문제점

du.study 2022. 3. 14. 23:46
728x90

간단한 통계 서비스를 구축하는 과정에서 맞이한 문제점을 정리해봅니다.

사용한 Spring version : 2.5.10

spring yaml 설정

spring:
  datasource:
    url: jdbc:h2:~/test_leisure_product;MODE=MySQL;DB_CLOSE_ON_EXIT=FALSE
	....
    type: com.zaxxer.hikari.HikariDataSource
  jpa:
    show-sql: true
    properties:
      hibernate:
        format_sql: true
        globally_quoted_identifiers: true
        dialect: org.hibernate.dialect.MySQL5InnoDBDialect
  	....

 

시작은 H2 SQLGrammarException: could not prepare statement 관련 에러로 시작을 헀습니다.

저는 JPA를 사용하면서 nativeQuery를 같이 사용하는 과정에서 발생했습니다. 제가 사용한 쿼리를 간략하게 요약해보면..

@Query(value = "SELECT\n" +
        "	:nowDate AS statisticsDate,\n" +
        "	.... , nativeQuery = true)
List<...> ...(@Param("nowDate") String nowDate, @Param("startDate") String startDate, @Param("endDate") String endDate);

변수를 select에 출력하고 별도 쿼리에서 사용하는과정에서 이슈가 발생했고, 실제 쿼리를 보면 이상한부분에 [*] 붙어있는 기괴한 현상이 발생했습니다.
해당문제는 h2를 mysql 모드로 돌리고, globally_quoted_identifiers: true 이 설정을 통해서 쿼리 문제를 해결할 수 있었습니다.

 

 

하지만 여전히 문제가 해결되지않았고, 아래의 class에 디버깅을 걸고나서야 원인을 정확하게 알 수 있었습니다.

org.h2.message.DbException getJdbcSQLException

실제 노출 되는 에러는 Class "org.locationtech.jts.geom.Geometry" not found 로 발생했고 이는 다음 스텍오버플로우 에서 힌드를 얻게 되었습니다.

 

에러를 보고서 좀더 찾아본 결과, org.h2.util.geometry.JTSUtils 를 사용하는 과정에서 라이브러리가 빠져있는것을 발견하였고 아래 설정을 추가하고 에러를 끝낼 수 있었습니다.

testImplementation("com.graphhopper.external:jackson-datatype-jts:1.0-2.7")

 

 

앞으로 h2관련 쿼리 에러는 로그가 제대로 안보이면 org.h2.message.DbException getJdbcSQLException 여기선에서도 꼭 확인을 해봐야곘습니다.

728x90
Comments