|
| 1 | +package org.baeldung.config; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | + |
| 5 | +import javax.sql.DataSource; |
| 6 | + |
| 7 | +import org.springframework.beans.factory.annotation.Autowired; |
| 8 | +import org.springframework.context.annotation.Bean; |
| 9 | +import org.springframework.context.annotation.Configuration; |
| 10 | +import org.springframework.context.annotation.PropertySource; |
| 11 | +import org.springframework.core.env.Environment; |
| 12 | +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; |
| 13 | +import org.springframework.jdbc.datasource.DriverManagerDataSource; |
| 14 | +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; |
| 15 | +import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; |
| 16 | + |
| 17 | +import com.google.common.base.Preconditions; |
| 18 | + |
| 19 | +@Configuration |
| 20 | +@PropertySource({ "classpath:persistence-multiple-db.properties" }) |
| 21 | +@EnableJpaRepositories(basePackages = "org.baeldung.persistence.multiple.dao.product", entityManagerFactoryRef = "productEntityManager", transactionManagerRef = "transactionManager") |
| 22 | +public class ProductConfig { |
| 23 | + @Autowired |
| 24 | + private Environment env; |
| 25 | + |
| 26 | + public ProductConfig() { |
| 27 | + super(); |
| 28 | + } |
| 29 | + |
| 30 | + @Bean(name = "productEntityManager") |
| 31 | + public LocalContainerEntityManagerFactoryBean productEntityManagerFactory() { |
| 32 | + final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); |
| 33 | + em.setDataSource(productDataSource()); |
| 34 | + em.setPackagesToScan(new String[] { "org.baeldung.persistence.multiple.model.product" }); |
| 35 | + |
| 36 | + final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); |
| 37 | + em.setJpaVendorAdapter(vendorAdapter); |
| 38 | + final HashMap<String, Object> properties = new HashMap<String, Object>(); |
| 39 | + properties.put("hibernate.transaction.jta.platform", MyJtaPlatform.class.getName()); |
| 40 | + properties.put("javax.persistence.transactionType", "JTA"); |
| 41 | + properties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto")); |
| 42 | + properties.put("hibernate.dialect", env.getProperty("hibernate.dialect")); |
| 43 | + em.setJpaPropertyMap(properties); |
| 44 | + |
| 45 | + return em; |
| 46 | + } |
| 47 | + |
| 48 | + @Bean |
| 49 | + public DataSource productDataSource() { |
| 50 | + final DriverManagerDataSource dataSource = new DriverManagerDataSource(); |
| 51 | + dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName"))); |
| 52 | + dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("product.jdbc.url"))); |
| 53 | + dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user"))); |
| 54 | + dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.pass"))); |
| 55 | + |
| 56 | + return dataSource; |
| 57 | + } |
| 58 | + |
| 59 | +} |
0 commit comments