본문 바로가기

웹개발/Spring

data.sql

H2 : in-memory DB, 따라서 Test 실행할 때 생성되고, Test 실행 끝나면 소멸됨

스프링에서 테스트할 때 기본적인 데이터 넣고 시작할 때 data.sql 이용하면 편리하다.

data.sql 을 /resources 하위에 두면 JPA가 로딩할 때 해당 쿼리들을 자동으로 한번 실행해줌.

 

+ application.properties 에서 설정해줘야하는 것들

# 기본적으로 data.sql 은 Hibernate 가 초기화 되기전에 수행된다.
# Hibernate 에서 생성한뒤 data.sql 이 수행되게 하려면 아래설정을 해주어야 한다
spring.jpa.defer-datasource-initialization=true

# JPA에서 생성되는 SQL 문을 콘솔 출력
spring.jpa.show-sql=true

# 쿼리가 훨씬 보기 좋게 정렬되어 로그 찍힌다
spring.jpa.properties.hibernate.format_sql=true

- data.sql

call next value for hibernate_sequence;   -- AI 값 증가
insert into user () values ();

 => call next value for hibernate_sequence; -> PK 위반을 막아줌. 

'웹개발 > Spring' 카테고리의 다른 글

[Security] 회원 가입  (0) 2021.12.13
[Security] Authentication, Authorization, 시큐리티 기본 메커니즘  (0) 2021.12.13
1: N 관계 매핑  (0) 2021.12.06
[JPA] Auto-Generated + PK 만드는 법  (0) 2021.12.06
Spring Validation  (0) 2021.11.30