Merge pull request #6877 from amit2103/BAEL-12800
[BAEL-12800] - Fixed the multiple DB article codebase
This commit is contained in:
commit
bd2793f09e
|
@ -0,0 +1,71 @@
|
||||||
|
package com.baeldung.multipledb;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.boot.jdbc.DataSourceBuilder;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Profile;
|
||||||
|
import org.springframework.context.annotation.PropertySource;
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
|
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||||
|
import org.springframework.orm.jpa.JpaTransactionManager;
|
||||||
|
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||||
|
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||||
|
import org.springframework.transaction.PlatformTransactionManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* By default, the persistence-multiple-db.properties file is read for
|
||||||
|
* non auto configuration in PersistenceProductConfiguration.
|
||||||
|
* <p>
|
||||||
|
* If we need to use persistence-multiple-db-boot.properties and auto configuration
|
||||||
|
* then uncomment the below @Configuration class and comment out PersistenceProductConfiguration.
|
||||||
|
*/
|
||||||
|
//@Configuration
|
||||||
|
@PropertySource({"classpath:persistence-multiple-db-boot.properties"})
|
||||||
|
@EnableJpaRepositories(basePackages = "com.baeldung.multipledb.dao.product", entityManagerFactoryRef = "productEntityManager", transactionManagerRef = "productTransactionManager")
|
||||||
|
@Profile("!tc")
|
||||||
|
public class PersistenceProductAutoConfiguration {
|
||||||
|
@Autowired
|
||||||
|
private Environment env;
|
||||||
|
|
||||||
|
public PersistenceProductAutoConfiguration() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public LocalContainerEntityManagerFactoryBean productEntityManager() {
|
||||||
|
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
||||||
|
em.setDataSource(productDataSource());
|
||||||
|
em.setPackagesToScan("com.baeldung.multipledb.model.product");
|
||||||
|
|
||||||
|
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
||||||
|
em.setJpaVendorAdapter(vendorAdapter);
|
||||||
|
final HashMap<String, Object> properties = new HashMap<String, Object>();
|
||||||
|
properties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
|
||||||
|
properties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));
|
||||||
|
em.setJpaPropertyMap(properties);
|
||||||
|
|
||||||
|
return em;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@ConfigurationProperties(prefix="spring.product")
|
||||||
|
public DataSource productDataSource() {
|
||||||
|
return DataSourceBuilder.create().build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public PlatformTransactionManager productTransactionManager() {
|
||||||
|
final JpaTransactionManager transactionManager = new JpaTransactionManager();
|
||||||
|
transactionManager.setEntityManagerFactory(productEntityManager().getObject());
|
||||||
|
return transactionManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,76 @@
|
||||||
|
package com.baeldung.multipledb;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.boot.jdbc.DataSourceBuilder;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Primary;
|
||||||
|
import org.springframework.context.annotation.Profile;
|
||||||
|
import org.springframework.context.annotation.PropertySource;
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
|
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||||
|
import org.springframework.orm.jpa.JpaTransactionManager;
|
||||||
|
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||||
|
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||||
|
import org.springframework.transaction.PlatformTransactionManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* By default, the persistence-multiple-db.properties file is read for
|
||||||
|
* non auto configuration in PersistenceUserConfiguration.
|
||||||
|
* <p>
|
||||||
|
* If we need to use persistence-multiple-db-boot.properties and auto configuration
|
||||||
|
* then uncomment the below @Configuration class and comment out PersistenceUserConfiguration.
|
||||||
|
*/
|
||||||
|
//@Configuration
|
||||||
|
@PropertySource({"classpath:persistence-multiple-db-boot.properties"})
|
||||||
|
@EnableJpaRepositories(basePackages = "com.baeldung.multipledb.dao.user", entityManagerFactoryRef = "userEntityManager", transactionManagerRef = "userTransactionManager")
|
||||||
|
@Profile("!tc")
|
||||||
|
public class PersistenceUserAutoConfiguration {
|
||||||
|
@Autowired
|
||||||
|
private Environment env;
|
||||||
|
|
||||||
|
public PersistenceUserAutoConfiguration() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
|
@Primary
|
||||||
|
@Bean
|
||||||
|
public LocalContainerEntityManagerFactoryBean userEntityManager() {
|
||||||
|
System.out.println("loading config");
|
||||||
|
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
||||||
|
em.setDataSource(userDataSource());
|
||||||
|
em.setPackagesToScan("com.baeldung.multipledb.model.user");
|
||||||
|
|
||||||
|
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
||||||
|
em.setJpaVendorAdapter(vendorAdapter);
|
||||||
|
final HashMap<String, Object> properties = new HashMap<String, Object>();
|
||||||
|
properties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
|
||||||
|
properties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));
|
||||||
|
em.setJpaPropertyMap(properties);
|
||||||
|
|
||||||
|
return em;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Primary
|
||||||
|
@Bean
|
||||||
|
@ConfigurationProperties(prefix="spring.user")
|
||||||
|
public DataSource userDataSource() {
|
||||||
|
return DataSourceBuilder.create().build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Primary
|
||||||
|
@Bean
|
||||||
|
public PlatformTransactionManager userTransactionManager() {
|
||||||
|
final JpaTransactionManager transactionManager = new JpaTransactionManager();
|
||||||
|
transactionManager.setEntityManagerFactory(userEntityManager().getObject());
|
||||||
|
return transactionManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,18 @@
|
||||||
|
# user data source
|
||||||
|
spring.user.driverClassName=org.h2.Driver
|
||||||
|
spring.user.jdbc-url=jdbc:h2:mem:spring_jpa_user;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS USERS
|
||||||
|
spring.user.username=sa
|
||||||
|
spring.user.password=sa
|
||||||
|
|
||||||
|
# product data source
|
||||||
|
spring.product.driverClassName=org.h2.Driver
|
||||||
|
spring.product.jdbc-url=jdbc:h2:mem:spring_jpa_product;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS PRODUCTS
|
||||||
|
spring.product.username=sa
|
||||||
|
spring.product.password=sa
|
||||||
|
|
||||||
|
# hibernate.X
|
||||||
|
hibernate.dialect=org.hibernate.dialect.H2Dialect
|
||||||
|
hibernate.show_sql=false
|
||||||
|
hibernate.hbm2ddl.auto=create-drop
|
||||||
|
hibernate.cache.use_second_level_cache=false
|
||||||
|
hibernate.cache.use_query_cache=false
|
Loading…
Reference in New Issue