Springbootのjpaマルチデータソース構成
1480 ワード
configuration
@Configuration
@EntityScan("com.codecraft.domain")
@EnableJpaRepositories(basePackages = "com.codecraft",entityManagerFactoryRef = "pgEntityManagerFactory",
transactionManagerRef = "pgTransactionManager")
public class JpaConfig {
@Autowired
@Qualifier("pgDataSource")
public DataSource pgDataSource;
@Bean
PlatformTransactionManager pgTransactionManager() {
return new JpaTransactionManager(pgEntityManagerFactory().getObject());
}
@Bean
LocalContainerEntityManagerFactoryBean pgEntityManagerFactory() {
HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
jpaVendorAdapter.setGenerateDdl(true);
jpaVendorAdapter.setDatabase(Database.POSTGRESQL);
jpaVendorAdapter.setDatabasePlatform("org.hibernate.dialect.PostgreSQLDialect");
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
factoryBean.setDataSource(pgDataSource);
factoryBean.setJpaVendorAdapter(jpaVendorAdapter);
factoryBean.setJpaPropertyMap(jpaProperties());
return factoryBean;
}
private Map jpaProperties() {
Map props = new HashMap<>();
props.put("hibernate.ejb.naming_strategy",new SpringNamingStrategy());
return props;
}
}