개발/spring

springboot 2.5.6 -> 2.7.6

방푸린 2023. 11. 13. 17:15
반응형

build.gradle

plugins {
    id 'org.springframework.boot' version '2.7.6'
    id 'io.spring.dependency-management' version '1.0.15.RELEASE'
    id 'java'
    id "org.sonarqube" version "3.0"
    id 'jacoco'
}

ext {
    querydslVersion = '5.0.0'
    set('springCloudVersion', "2021.0.5")
}

수정하고 빌드하고 프로그램 시작하면 아래와 같은 에러가 날 텐데..

org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; 
nested exception is java.lang.NullPointerException

application.properties에 아래 추가

spring.mvc.pathmatch.matching-strategy=ANT_PATH_MATCHER

테스트 돌릴 때 아래와 같은 에러가 나면.. 테스트 application.properties에도 잊지 말고 추가

Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang
.NullPointerException: Cannot invoke "org.springframework.web.servlet.mvc.condition
.PatternsRequestCondition.getPatterns()" because "this.condition" is null

원인

path maching 기본 전략이 2.6부터 ant matching -> path pattern으로 변경됨

AntPathMatcher는 간단하고 기존의 코드와의 호환성을 제공하는 반면, PathPattern은 성능과 유연성 측면에서 더 우수합니다. 스프링 부트 2.6.0부터 기본 매칭 전략으로 PathPattern이 도입되었으며, 보다 복잡한 URL 구조를 효율적으로 처리할 수 있도록 돕습니다.

 


 

h2 test 에러

springboot2.5.6; h2 1.4.200 

h2로 레파지토리 테스트를 하고 있다. 아래 entity가 있으면 테스트 실행 시 아래 쿼리가 자동으로 실행되면서 테이블이 생성된다.

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private BigInteger seq;
 create table `item_withdraw_log` (
       `seq` decimal(19,2) not null auto_increment,
       ...

이렇게 잘 사용하고 있었는데.. 버전업을 하고 테스트가 실패한다.

 

springboot2.7.6; h2 2.1.214

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private BigInteger seq;
Caused by: org.h2.jdbc.JdbcSQLFeatureNotSupportedException: Feature not supported: "DECIMAL(19, 2)"; SQL statement:

같은 create 쿼리를 실행하는데 decimal관련 에러가 난다.

 

mysql bigint는.. 꼭..?! 자바의 long으로 해야 하는 듯..

아니면 아래 어노테이션 추가...

@Column(precision = 100, scale = 0)
//100은 좀 심하고 디비의 최대 자릿수 정도로 하면 될 듯

허나.. 테스트를 위한 어노테이션은 혼동스러울 것 같아 BigInteger -> Long을 변환하는 게 나을 것 같다.

https://www.h2database.com/html/migration-to-v2.html

 

Migration to 2.0

Contents Introduction Upgrading File Format Data types Identity columns and sequences INFORMATION_SCHEMA General Introduction Between version 1.4.200 and version 2.0.202 there have been considerable changes, such that a simple update is not possible. It wo

www.h2database.com


jpa native query 에러

2.5.6 버전에서 사용하고 있던.. 잘 돌아가던 쿼리가 있다.

  @Query(value = "SELECT * FROM hd_user_leaderboard_data_log AS huldl " + "WHERE huldl.gid = :gid AND huldl.base_date > :baseDate "
      + "AND huldl.leaderboard_id IN :leaderboardIds " + "AND huldl.extra_data ->> '$.ArenaType' = :arenaType ORDER BY huldl.log_date DESC ",
         nativeQuery = true)
Page<UserLeaderboardDataLog> findAllByGidAndBaseDateAfterAndArenaTypeAndLeaderboardIdNotIn(String gid,
    LocalDateTime baseDate,
    String arenaType,
    Set<Integer> leaderboardIds,
    Pageable pageable);

 

컴파일 시 문제는 없었는데 서버 시작 후 api를 호출하니 아래와 같은 에러가 발생..

Caused by: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
..
Caused by: java.sql.SQLSyntaxErrorException: Unknown column 'huldl' in 'field list'
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
    ..

심지어 하이버네이트 실행 쿼리가 지나가고, 그걸 긁어서 db client 툴에 붙여 넣고 실행하면 잘 나온다.

사용하던 쿼리기도 하고 쿼리 자체에는 문제가 없다는 사실 확인..

그러면 왜 나는 거지..

해결 1.

에러 내용이 huldl 관련 에러라.. 우선 alias를 지워본다..

@Query(value = "SELECT * FROM leaderboard_data_log " + "WHERE gid = :gid AND base_date > :baseDate "
    + "AND leaderboard_id IN :leaderboardIds " + "AND extra_data ->> '$.ArenaType' = :arenaType ORDER BY log_date DESC ", nativeQuery = true)

성공..

해결 2.

다른 글을 보니 alias에 backtick(`)를 사용하면 된다고 해서.. 해봤는데 잘 된다..

@Query(value = "SELECT * FROM hd_user_leaderboard_data_log AS `huldl` " + "WHERE `huldl`.gid = :gid AND `huldl`.base_date > :baseDate "
    + "AND `huldl`.leaderboard_id IN :leaderboardIds " + "AND `huldl`.extra_data ->> '$.ArenaType' = :arenaType ORDER BY `huldl`.log_date DESC ",
       nativeQuery = true)

https://stackoverflow.com/questions/74055494/native-query-does-not-recognized-in-query-in-spring-data-jpa

 

Native query does not recognized in @Query in spring data jpa

When I define a query method in ContactRepository and write a native mysql query in @Query, it gives the following error. What is the reason? Can't I write an alias for the table? @Repository public

stackoverflow.com

해결 3.

alias를 asd로 바꿔보니 잘된다..ㅋㅋ 역시 huldl이 뭔가 있음

@Query(value = "SELECT * FROM hd_user_leaderboard_data_log AS asd " + "WHERE asd.gid = :gid AND asd.base_date > :baseDate "
    + "AND asd.leaderboard_id IN :leaderboardIds " + "AND asd.extra_data ->> '$.ArenaType' = :arenaType ORDER BY asd.log_date DESC ",
       nativeQuery = true)

 

아무래도 버전 2.7.6으로 바꾸면서 alias로 둔 huldl이 잘못 변환되거나 예약어 거나 그런 것 같다.

 

*) 참고로 native query = false 인 기본 jpa에서 콜롬/테이블 명에 나도 모르게 예약어를 사용하여 에러가 날 수도 있는데, 이를 방지하기 위해서 아래 설정을 추가하면 jpa에서 쿼리 만들어서 실행할 때 해당 이름에 알아서 backtick을 붙여서 날려준다.

spring.jpa.properties.hibernate.globally_quoted_identifiers=true

 


참고

https://chartio.com/learn/sql-tips/single-double-quote-and-backticks-in-mysql-queries/

 

Single Quote, Double Quote, and Backticks in MySQL Queries

Using Backticks, Double Quotes, and Single Quotes when querying a MySQL database can be boiled down to two basic points: Quotes (Single and Double) are used …

chartio.com

 

728x90
반응형