Spring Boot Multiple Datasource - 只能使用一个

时间:2015-04-07 06:24:02

标签: java hibernate spring-boot spring-java-config

我有问题,当我尝试使用2个数据库时出现错误

我的错误日志:

  

NoUniqueBeanDefinitionException:没有定义[javax.sql.DataSource]类型的限定bean:期望的单个匹配bean但找到2:reportDataSource,secondDataSource

然后,如果我在第一个数据源上设置@Primary,我会在第二个数据库查询时出错:

  

o.h.engine.jdbc.spi.SqlExceptionHelper:SQL错误:942,SQLState:42000   o.h.engine.jdbc.spi.SqlExceptionHelper:ORA-00942:表或视图不存在

     

org.springframework.dao.InvalidDataAccessResourceUsageException:无法提取ResultSet; SQL [不适用];嵌套异常是org.hibernate.exception.SQLGrammarException:无法提取ResultSet

如果我在DB上手动运行它,那么SQL工作。

Hibernete没有看到我的表没有@Primary数据源。当我从app第二个数据库删除工作正常。 在我看来,两个实体经理使用@Primary数据源,这就是为什么Hibernate不会从第二个看到表格的。

我不知道我现在该做什么。欢迎任何帮助。

First 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();
    }
}

第一个数据库的存储库(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);
}

第二个数据库的存储库(com.company.db.second.repository.BvpRepository):

@Repository
@Table(name = "BVP")
public interface BvpRepository extends JpaRepository<Bvp, Long> {

    List<Bvp> findAll();

    Bvp save(Bvp entity);
}

第一个数据库的实体位于com.company.db.report.entity中, 对于com.company.db.second.entity中的第二个DB。

类似的问题是there,但这个答案并没有帮助。

1 个答案:

答案 0 :(得分:0)

如果您需要检查配置,请查看此link以重构并使您的代码更具可读性。

我可以看到您在存储库中使用@Table。必须在表示数据库中的表的实体中使用@Table。在您的情况下,将是BvpDbUser类。