SpringBoot에서 jpa를 사용하여 Entity를 생성시에 @Entity 어노테이션을 사용하여 DDL이 자동으로 생성되도록 설정을 하였고
초기 데이터 적재를 위해 resource 디렉토리 아래 data.sql 파일에 insert 문을 넣고 실행시 아래 에러가 발생했다.
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 URL [file:/Users/guda/dev/SpringBoot/workspace/demo/out/production/resources/data.sql]: INSERT INTO book (`title`, `author`, `price`) VALUES ('지금 이대로 좋다', '법류 저', 9330); nested exception is org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "BOOK" not found; SQL statement:
기본적으로 data.sql 스크립트는이제 Hibernate가초기화되기전에실행된다. data.sql을사용하여 Hibernate에의해생성된스키마를채우려면 spring.jpa.defer-datasource-initialization을 true로설정해라. 데이터베이스 초기화 기술을 혼합하는 것은 권장되지 않지만이는 data.sql을통해채우기전에하이버네이트에서생성한스키마를기반으로 schema.sql 스크립트를사용할수도있습니다.
Hibernate and data.sql
By default,data.sqlscripts 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 usedata.sqlto populate a schema created by Hibernate, setspring.jpa.defer-datasource-initializationtotrue. While mixing database initialization technologies is not recommended, this will also allow you to use aschema.sqlscript to build upon a Hibernate-created schema before it’s populated viadata.sql.