[Spring-error해결] data.sql을 만들 때 에러

99winnmin·2022년 7월 18일
0

Spring-error이슈

목록 보기
2/4
post-thumbnail

버전 오류는 공식 문서를 애용하자!

https://docs.spring.io/spring-boot/docs/2.7.1/reference/htmlsingle/

call next value for hibernate_sequence;
insert into JpaUser(`id`,`name`,`email`,`created_at`,`updated_at`) values (1,'ryu','ojysep1@gmail.com',now(),now());

call next value for hibernate_sequence;
insert into jpauser(`id`,`name`,`email`,`created_at`,`updated_at`) values (2,'seung','ojysep2@gmail.com',now(),now());

call next value for hibernate_sequence;
insert into jpauser(`id`,`name`,`email`,`created_at`,`updated_at`) values (3,'min','ojysep3@gmail.com',now(),now());

call next value for hibernate_sequence;
insert into jpauser(`id`,`name`,`email`,`created_at`,`updated_at`) values (4,'yoo','ojysep4@gmail.com',now(),now());

call next value for hibernate_sequence;
insert into jpauser(`id`,`name`,`email`,`created_at`,`updated_at`) values (5,'springboot','ojysep5@gmail.com',now(),now());

Spring JPA를 학습하던중 테스트 데이터를 만들기 위해 다음과 같은 코드를 작성했지만 오류가 발생했다.

Sequence "HIBERNATE_SEQUENCE" not found; SQL statement:
call next value for hibernate_sequence [90036-214]

다음과 같은 내용으로 그런 문법이 아예없다고 하던 것이다...강의에서 본 것을 그대로 따라친 것인데 매우 당혹스러웠는데 대게 이런 경우 버전 차이 때문이지 않을까해서 검색을 시작했다.
제일 좋은 것은 공식문서에서 보는 것이기에

다음과 같은 내용을 찾았는데 해당 내용은 데이터 초기화하는 방식에 차이가 있는 듯했다. 설명에 나와있는대로 application.yaml에 다음 내용을 추가하니 테스트코드가 잘 실행되었다.

spring:
	jpa:
    	defer-datasource-initialization: true

하지만 잘 이해가 되지 않아 검색을 좀 더 해보았는데 한 블로그에서

버전이 2.4 -> 2.5로 변경될 때 실행순서가 바뀌었다...

2.4 버전까지는 Hibernate 초기화 -> data.sql script 실행이었으나,

2.5 버전부터는 data.sql script 실행 -> Hibernate 초기화라... Hibernate 객체를 찾지 못한 것이다.

이런 내용을 찾았고 현재 Spring-Boot 2.7.1을 사용하고 있는 나는 data.sql를 사용할 시 꼭 고려해야할 점이였던 것이다. defer-datasource-initialization: true 설정이 hibernate 초기화 후 data.sql script를 동작하도록해주는 설정인 듯 하다.

profile
功在不舍

0개의 댓글