我有問題,當我嘗試使用2個數據庫我得到的錯誤春天引導多個數據源 - 工作只有一個
我的錯誤日誌:
NoUniqueBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] is defined: expected single matching bean but found 2: reportDataSource,secondDataSource
然後如果我設置@Primary在第一數據源,我得到第二個數據庫查詢出錯:
o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 942, SQLState: 42000 o.h.engine.jdbc.spi.SqlExceptionHelper : ORA-00942: table or view does not exist
org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet
如果我在數據庫上運行它,SQL工作會變得很麻煩。
Hibernete沒有看到我的表不是@Primary數據源。當我從應用程序第二次工作正常刪除第一個數據庫。 在我看來,兩個Entity Mangers都使用@Primary數據源,這就是爲什麼Hibernate從第二個看不到表。
我不知道我現在應該做什麼。任何幫助將受到歡迎。
首先DBCONFIG:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "reportEntityMangerFactory",
basePackages = {"com.company.db.report.repository"}
)
public class ReportDbConfig {
@Bean(name = "reportDataSource")
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
dataSource.setUrl("jdbc:oracle:thin:@127.0.0.1:1521:FIRST");
dataSource.setUsername("test");
dataSource.setPassword("test");
return dataSource;
}
@Bean(name = "reportEntityManager")
public EntityManager entityManager() {
return entityManagerFactory().createEntityManager();
}
@Bean(name = "reportEntityMangerFactory")
public EntityManagerFactory entityManagerFactory() {
HashMap<String, Object> properties = new HashMap<String, Object>();
properties.put("hibernate.hbm2ddl.auto", "validate");
properties.put("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect");
HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setDataSource(dataSource());
factory.setJpaVendorAdapter(jpaVendorAdapter);
factory.setPackagesToScan("com.company.db.report.entity");
factory.setJpaPropertyMap(properties);
factory.afterPropertiesSet();
return factory.getObject();
}
}
二對DBConfig:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "secondEntityMangerFactory",
basePackages = {"com.company.db.second.repository"}
)
public class SecondDbConfig {
@Bean(name = "secondDataSource")
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
dataSource.setUrl("jdbc:oracle:thin:@127.0.0.1:1521:SECOND");
dataSource.setUsername("test");
dataSource.setPassword("test");
return dataSource;
}
@Bean(name = "secondEntityManager")
public EntityManager entityManager() {
return entityManagerFactory().createEntityManager();
}
@Bean(name = "secondEntityMangerFactory")
public EntityManagerFactory entityManagerFactory() {
HashMap<String, Object> properties = new HashMap<String, Object>();
properties.put("hibernate.hbm2ddl.auto", "validate");
properties.put("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect");
HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setDataSource(dataSource());
factory.setJpaVendorAdapter(jpaVendorAdapter);
factory.setPackagesToScan("com.company.db.second.entity");
factory.setJpaPropertyMap(properties);
factory.afterPropertiesSet();
return factory.getObject();
}
}
從1 DB(com.company.db.report.repository.DbUserRepository)庫:
@Repository
@Table(name = "DB_USER")
public interface DbUserRepository extends JpaRepository<DbUser, Long> {
List<DbUser> findAll();
DbUser save(DbUser entity);
}
庫from 2nd DB(com.company.db.second.repository.BvpRepository):
@Repository
@Table(name = "BVP")
public interface BvpRepository extends JpaRepository<Bvp, Long> {
List<Bvp> findAll();
Bvp save(Bvp entity);
}
第1個數據庫的實體位於com.company.db.report.entity中,第2個數據庫位於com.company.db.second.entity中, 。
類似的問題是there,但這個答案沒有幫助。
Thx,它現在正在工作。在你的例子中,你應該爲emf2添加Bean名稱。 – Son
它不需要bean的名字,它是方法的名字。 –
很高興知道,thx :) – Son