목차

Spring Boot And JPA

Open Entity Manager In View / Open Session In View (OSIV) 끄기

spring.jpa.open-in-view: false
// SpringBoot 2.1 의 org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.JpaWebConfiguration 참조.
 
@ConditionalOnProperty(prefix = "spring.jpa", name = "open-in-view", havingValue = "true", matchIfMissing = true)
protected static class JpaWebConfiguration {

별도 설정하기

// 최상단에
@EnableConfigurationProperties
@EnableJpaRepositories(basePackageClasses = MyRepository.class,
        entityManagerFactoryRef = "entityManagerFactory",
        transactionManagerRef = "transactionManager")
 
... 
@Bean
@ConfigurationProperties(prefix = "my.datasource")
public DataSource dataSource() {
    return DataSourceBuilder.create()
            .type(org.apache.tomcat.jdbc.pool.DataSource.class) // Tomcat Pool 사용시
            .build();
}
 
@Bean
@ConfigurationProperties(prefix = "my.jpa")
public JpaProperties jpaProperties() {
    return new JpaProperties();
}
 
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    Map<String, String> hibernateProperties = jpaProperties().getHibernateProperties(dataSource());
 
    return new EntityManagerFactoryBuilder(new HibernateJpaVendorAdapter(), hibernateProperties, null)
            .dataSource(dataSource)
            .persistenceUnit("mypersistenceunit")
            .packages(EntityClasses.class) // entity class들의 위치
            .build();
}
my:
  jpa:
    database-platform: MYSQL
    properties:
      hibernate:
        dialect: org.hibernate.dialect.MySQL57InnoDBDialect
        default_batch_fetch_size: 16
        id.new_generator_mappings: true
        format_sql: true
        show_sql: false
        use_sql_comments: true
        hbm2ddl.auto: update

Naming Strategy