Spring

Spring Boot 2.5 이후 버전과 Hibernate 와 data.sql

728x90

Spring Cloud 강의를 들으면서 H2 데이터베이스와 Spring Data JPA 를 사용했는데 

미리 넣어둘 데이터를 data.sql 에 정의해두고 테이블 생성은 Hibernate 에 의해서 하도록 하려고 하였으나 

테이블이 생성되지 않고 에러가 났다.

 

결론부터 말하면 

application.yml 에 다음의 요소를 추가하면 에러가 해결된다

spring.jpa.defer-datasource-initialization: true
cs

 

2.5 이후 다음과 같은 내용이 추가되었다

 

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.

 

Hibernate 가 테이블을 만들기 이전에 data.sql 이 먼저 실행되도록 바뀐것 같다

Flyway 와 Liquibase 가 아직은 뭔지는 모르겠으나 이것과 일치시키기 위해서 바뀌었다고 하는것 같다

그래서 Hibernate 가 먼저 테이블을 만들게 하고 싶으면 spring.jpa.defer-datasource-initialization 을 true 하라는 내용이 있다.

또 Hibernate 대신에 schema.sql 을 직접 작성해서 스키마를 만드는 방법도 있는듯하다

 

728x90

'Spring' 카테고리의 다른 글

Circular Dependency  (0) 2021.09.17
Spring Cloud Eureka 와 기본 REST API  (0) 2021.09.14
Spring Boot 와 JWT 로 로그인하기  (0) 2021.07.06
Spring Boot 로 이메일 회원가입 하기  (0) 2021.07.06
Spring Cloud Netflix Eureka  (0) 2021.07.05