[스프링부트] .sql 파일 추가 후 발생하는 에러

지윤·2023년 5월 26일
0

문제 발생

Failed to execute SQL script statement

더미 데이터를 만들어주기 위해 data.sql 파일을 추가하고 서버를 실행시켰는데, 기존에 뜨지 않던 에러가 떴다.
그 말인 즉슨 sql 파일이 원인이라는 것 !!!!

다행히 구글링을 통해 쉽게 해결할 수 있었다. 다른 분들도 자주 겪으시는 문제인 것 같다 🥲

문제 원인

Hibernate and data.sql
By default, data.sql scripts are now run before Hibernate is initialized. This aligns the behavior of basic script-based initialization with that of Flyway and Liquibase. If you want to use data.sql to populate a schema created by Hibernate, set spring.jpa.defer-datasource-initialization to true. While mixing database initialization technologies is not recommended, this will also allow you to use a schema.sql script to build upon a Hibernate-created schema before it’s populated via data.sql.

Spring Boot가 2.4.x에서 2.5.x로 버전이 업데이트 되었고 그 과정에서 생긴 변동사항이 원인이였다.

Spring Boot 2.5 버전부터는 스크립트 기반 초기화의 동작 과정을 Flyway, Liquibase와 일치시키기 위해 data.sql가 Hibernate 초기화 전에 실행된다.

2.5.x 버전부터는 spring.datasourece.initialization.mode가 depreated로 표시됨에 따라 spring.sql.init.mode로 대체 sql 관련 공통화 부분을 spring.sql로 따로 분리시켰다.

그러므로 아래의 방법으로 문제를 해결하면 된다.

문제 해결

  1. resources > application.properties 로 들어간다.

  2. spring.jpa.defer-datasource-initialization 옵션 값을 true 로 추가해 준다.

다른 해결 방법으로는 schema.sql을 추가하여 hibernate가 스키마를 생성하는 과정보다 먼저 실행되도록 하여 스키마에 data.sql을 채우는 방법도 있다. 하지만 DB 초기화 기술을 혼합하여 사용하는 것은 권장하지 않는다고 한다.



참고

스프링 부트 2.5 업데이트 : hibernate, data.sql 관련 변동사항
Spring Boot 2.5 Release Notes

profile
떠돌이 컴공

0개의 댓글