Мне нужно получить доступ к 2 различным схемам в одной базе данных (MySQL).
Я написал здесь два класса конфигурации:
package twodb.webfi.config;
import java.util.Properties;
import javax.persistence.EntityManagerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver;
import org.springframework.orm.hibernate4.HibernateExceptionTranslator;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
@Configuration
@ComponentScan
@EnableTransactionManagement
@EnableJpaRepositories(basePackages={"twodb.webfi","twodb.mc"},
entityManagerFactoryRef = "entityManagerFactory1",
transactionManagerRef = "transactionManager1",
considerNestedRepositories = true)
public class FirstConfig {
@Autowired
private Environment env;
@Bean
public PlatformTransactionManager transactionManager1()
{
EntityManagerFactory factory = entityManagerFactory1().getObject();
return new JpaTransactionManager(factory);
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory1()
{
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setGenerateDdl(Boolean.TRUE);
vendorAdapter.setShowSql(Boolean.TRUE);
factory.setDataSource(dataSource1());
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan(new String[] {"twodb.webfi.entities","twodb.mc.model"});
Properties jpaProperties = new Properties();
jpaProperties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.ddl-auto"));
jpaProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
jpaProperties.setProperty("hibernate.show_sql", env.getProperty("hibernate.showSql"));
factory.setJpaProperties(jpaProperties);
factory.afterPropertiesSet();
factory.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver());
return factory;
}
@Bean
public HibernateExceptionTranslator hibernateExceptionTranslator()
{
return new HibernateExceptionTranslator();
}
@Bean(destroyMethod = "close")
public HikariDataSource dataSource1() {
System.out.println("<--HikariDataSource1-->");
HikariConfig dataSourceConfig = new HikariConfig();
dataSourceConfig.setDriverClassName(env.getProperty("ui.db.driver"));
dataSourceConfig.setJdbcUrl(env.getProperty("ui.db.url"));
dataSourceConfig.setUsername(env.getProperty("ui.db.username"));
dataSourceConfig.setPassword(env.getProperty("ui.db.password"));
dataSourceConfig.setMaximumPoolSize(env.getProperty("hikari.maximumPoolSize", Integer.class, new Integer(1)));
dataSourceConfig.setMinimumIdle(env.getProperty("hikari.minimumIdle", Integer.class, new Integer(1)));
dataSourceConfig.setIdleTimeout(env.getProperty("hikari.idleTimeout", Long.class, new Long(600000))); // 10 min
dataSourceConfig.setMaxLifetime(env.getProperty("hikari.maxLifetime", Long.class, new Long(1800000))); // 30 min
dataSourceConfig.setPoolName(env.getProperty("hikari.poolName", "upiCp"));
return new HikariDataSource(dataSourceConfig);
}
}
Другой класс конфигурации здесь:
package twodb.webfi.config;
import java.util.Properties;
import javax.persistence.EntityManagerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver;
import org.springframework.orm.hibernate4.HibernateExceptionTranslator;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
@Configuration
@ComponentScan
@EnableTransactionManagement
@EnableJpaRepositories(basePackages={"twodb.webfi","twodb.mc"},
entityManagerFactoryRef = "entityManagerFactory2",
transactionManagerRef = "transactionManager2",
considerNestedRepositories = true)
public class SecondConfig {
@Autowired
private Environment env;
@Bean
public PlatformTransactionManager transactionManager2()
{
EntityManagerFactory factory = entityManagerFactory2().getObject();
return new JpaTransactionManager(factory);
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory2()
{
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setGenerateDdl(Boolean.TRUE);
vendorAdapter.setShowSql(Boolean.TRUE);
factory.setDataSource(dataSource2());
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan(new String[] {"twodb.webfi.entities","twodb.mc.model"});
Properties jpaProperties = new Properties();
jpaProperties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.ddl-auto"));
jpaProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
jpaProperties.setProperty("hibernate.show_sql", env.getProperty("hibernate.showSql"));
factory.setJpaProperties(jpaProperties);
factory.afterPropertiesSet();
factory.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver());
return factory;
}
@Bean
public HibernateExceptionTranslator hibernateExceptionTranslator()
{
return new HibernateExceptionTranslator();
}
@Bean(destroyMethod = "close")
public HikariDataSource dataSource2() {
System.out.println("<--HikariDataSource2-->");
HikariConfig dataSourceConfig = new HikariConfig();
dataSourceConfig.setDriverClassName(env.getProperty("web.db.driver"));
dataSourceConfig.setJdbcUrl(env.getProperty("web.db.url"));
dataSourceConfig.setUsername(env.getProperty("web.db.username"));
dataSourceConfig.setPassword(env.getProperty("web.db.password"));
dataSourceConfig.setMaximumPoolSize(env.getProperty("hikari.maximumPoolSize", Integer.class, new Integer(1)));
dataSourceConfig.setMinimumIdle(env.getProperty("hikari.minimumIdle", Integer.class, new Integer(1)));
dataSourceConfig.setIdleTimeout(env.getProperty("hikari.idleTimeout", Long.class, new Long(600000))); // 10 min
dataSourceConfig.setMaxLifetime(env.getProperty("hikari.maxLifetime", Long.class, new Long(1800000))); // 30 min
dataSourceConfig.setPoolName(env.getProperty("hikari.poolName", "upiCp"));
return new HikariDataSource(dataSourceConfig);
}
}
Здесь я получаю исключение, как
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webCommonDaoImpl': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'javax.persistence.EntityManagerFactory' available: expected single matching bean but found 2: entityManagerFactory1,entityManagerFactory
Может кто-нибудь указать мне правильное направление использования нескольких баз данных с использованием JPA + Hibernate с Spring-boot?
Я ui
и web
схемы в MySQL.Оба являются одной и той же базой данных.