Spring JPA Entity create 스크립트 만들기

Table of Contents

1 code

spring:
  jpa:
    properties:
      javax:
	persistence:
	  schema-generation:
	    create-source: metadata
	    scripts:
	      action: create
	      create-target: src/main/resources/create.sql
	      drop-target: src/main/resources/drop.sql

2 설명

Spring은 벤더 독립적인 configuration options 을 제공한다. (ex, SQL 로깅) 일부는 컨텍스트에 따라 자동으로 감지되므로 설정할 필요가 없다.

spring.jpa.hibernate.ddl-auto 는 스페셜 케이스다, 왜냐하면 런타임 컨디션에 따라 다른 기본값을 갖기 때문이다. 내장형 DB를 사용하고 Liquibase나 Flyway 같은 스키마 관리툴이 DataSource 를 관리하지 않는 경우, 디폴트는 create-drop 이며, 다른 모든 경우에는 none 이다.

dialect(사용언어) 는 JPA provider가 감지한다. 언어를 직접 설정하고 싶으면, spring.jpa.database-platform 프로퍼티를 설정하라.

가장 일반적인 옵션은 다음 예다.

spring.jpa.hibernate.naming.physical-strategy=com.example.MyPhysicalNamingStrategy
spring.jpa.show-sql=true
---
spring:
  jpa:
    hibernate:
      naming:
	physical-strategy: "com.example.MyPhysicalNamingStrategy"
    show-sql: true

더해서 spring.jpa.properties.* 의 모든 속성은 local EntityManagerFactory 가 생성될 때 normal JPA properties로 전달된다.

3 유의사항

#+BEGINQUOTE You need to ensure that names defined under spring.jpa.properties.* exactly match those expected by your JPA provider. Spring Boot will not attempt any kind of relaxed binding for these entries.

For example, if you want to configure Hibernate's batch size you must use spring.jpa.properties.hibernate.jdbc.batch_size. #ENDQUOTE

4 Tip

Hibernate 프로퍼티에 advanced customization 을 적용해야 한다면, HibernatePropertiesCustomizer 빈을 등록해서 사용해라. 이것은 EntityManagerFactory 가 생성되기 전에 호출될 것이다. 이는 auto-configuration 에 의해 적용되는 모든 항목에 우선한다. (This takes precedence to anything that is applied by the auto-configuration)

Date: 2023-05-16 Tue 00:00

Author: Younghwan Nam

Created: 2024-04-16 Tue 10:06

Emacs 27.2 (Org mode 9.4.4)

Validate