Hibernate 초기화 전 data.sql 먼저 실행되어 발생하는 오류

Feb 21, 2024
Hibernate 초기화 전 data.sql 먼저 실행되어 발생하는 오류
Contents
해결
 
notion image
 
insert into user_tb(username, password, email, created_at) values('ssar', '1234', 'ssar@nate.com', now()); insert into user_tb(username, password, email, created_at) values('cos', '1234', 'cos@nate.com', now());
 
data.sql 에 쿼리문을 작성한 후 서버를 실행하면 다음과 같은 오류가 뜬다.
 
notion image
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: Invocation of init method failed; nested exception is org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of class path resource [db/data.sql]: insert into user_tb(username, password, email, created_at) values('ssar', '1234', 'ssar@nate.com', now()); nested exception is org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "USER_TB" not found (this database is empty); SQL statement:
 
Table "USER_TB" not found
 
테이블이 생성되기 전보다 insert 문이 실행되어서 오류가 발생한다.

해결

 
notion image
 
defer-datasource-initialization: true 추가
Share article

{CODE-RYU};