Jpa Multi-Data Source Configuration of springboot

  springboot

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<String, Object> jpaProperties() {
        Map<String, Object> props = new HashMap<>();
        props.put("hibernate.ejb.naming_strategy",new SpringNamingStrategy());
        return props;
    }


}