|  |  | @@ -0,0 +1,77 @@ | 
    
    |  |  | package com.pascaldimassimo.xyz; | 
    
    |  |  | 
 | 
    
    |  |  | import java.util.HashMap; | 
    
    |  |  | import java.util.Map; | 
    
    |  |  | 
 | 
    
    |  |  | import javax.persistence.EntityManagerFactory; | 
    
    |  |  | import javax.sql.DataSource; | 
    
    |  |  | 
 | 
    
    |  |  | import org.hibernate.cfg.ImprovedNamingStrategy; | 
    
    |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
    
    |  |  | import org.springframework.beans.factory.annotation.Qualifier; | 
    
    |  |  | import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; | 
    
    |  |  | import org.springframework.boot.autoconfigure.orm.jpa.EntityManagerFactoryBuilder; | 
    
    |  |  | import org.springframework.boot.context.properties.ConfigurationProperties; | 
    
    |  |  | import org.springframework.context.annotation.Bean; | 
    
    |  |  | import org.springframework.context.annotation.Configuration; | 
    
    |  |  | import org.springframework.context.annotation.Primary; | 
    
    |  |  | import org.springframework.orm.jpa.JpaTransactionManager; | 
    
    |  |  | import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; | 
    
    |  |  | import org.springframework.transaction.PlatformTransactionManager; | 
    
    |  |  | 
 | 
    
    |  |  | @Configuration | 
    
    |  |  | public class DataSourceConfig { | 
    
    |  |  | 
 | 
    
    |  |  | @Bean | 
    
    |  |  | @Primary | 
    
    |  |  | @ConfigurationProperties(prefix = "spring.datasource.prod") | 
    
    |  |  | public DataSource prodDataSource() { | 
    
    |  |  | return DataSourceBuilder.create().build(); | 
    
    |  |  | } | 
    
    |  |  | 
 | 
    
    |  |  | @Bean | 
    
    |  |  | @ConfigurationProperties(prefix = "spring.datasource.dev") | 
    
    |  |  | public DataSource devDataSource() { | 
    
    |  |  | return DataSourceBuilder.create().build(); | 
    
    |  |  | } | 
    
    |  |  | 
 | 
    
    |  |  | @Bean(name = "entityManagerFactory") | 
    
    |  |  | @Primary | 
    
    |  |  | public LocalContainerEntityManagerFactoryBean prodEntityManagerFactory( | 
    
    |  |  | EntityManagerFactoryBuilder builder) { | 
    
    |  |  | return builder | 
    
    |  |  | .dataSource(prodDataSource()) | 
    
    |  |  | .packages("com.pascaldimassimo.xyz") | 
    
    |  |  | .persistenceUnit("prod") | 
    
    |  |  | .properties(buildProperties()) | 
    
    |  |  | .build(); | 
    
    |  |  | } | 
    
    |  |  | 
 | 
    
    |  |  | @Bean(name = "devEntityManagerFactory") | 
    
    |  |  | public LocalContainerEntityManagerFactoryBean devEntityManagerFactory( | 
    
    |  |  | EntityManagerFactoryBuilder builder) { | 
    
    |  |  | return builder | 
    
    |  |  | .dataSource(devDataSource()) | 
    
    |  |  | .packages("com.pascaldimassimo.xyz") | 
    
    |  |  | .persistenceUnit("dev") | 
    
    |  |  | .properties(buildProperties()) | 
    
    |  |  | .build(); | 
    
    |  |  | } | 
    
    |  |  | 
 | 
    
    |  |  | private Map<String, Object> buildProperties() { | 
    
    |  |  | // This is usually set by HibernateJpaAutoConfiguration | 
    
    |  |  | Map<String, Object> properties = new HashMap<String, Object>(); | 
    
    |  |  | properties.put("hibernate.ejb.naming_strategy", | 
    
    |  |  | ImprovedNamingStrategy.class.getName()); | 
    
    |  |  | return properties; | 
    
    |  |  | } | 
    
    |  |  | 
 | 
    
    |  |  | @Bean | 
    
    |  |  | @Autowired | 
    
    |  |  | @Qualifier("devEntityManagerFactory") | 
    
    |  |  | public PlatformTransactionManager transactionManager(EntityManagerFactory emf) { | 
    
    |  |  | JpaTransactionManager txManager = new JpaTransactionManager(); | 
    
    |  |  | txManager.setEntityManagerFactory(emf); | 
    
    |  |  | return txManager; | 
    
    |  |  | } | 
    
    |  |  | } |