使用2个hibernate sessionFactories的Spring配置

时间:2015-10-19 14:35:39

标签: java hibernate spring-mvc

我想使用2个hibernate数据库连接。我正面临这个错误,我试图解决,但无法找到方法。我已经制作了两个不同的配置文件,并注释了@bean和@qualifires,但它仍然有效。

错误

By

我的配置:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'transactionManager' defined in class path resource [com/project/configuration/RepositoryConfiguration.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.hibernate.SessionFactory]: : No qualifying bean of type [org.hibernate.SessionFactory] is defined: expected single matching bean but found 2: sessionFactory2,sessionFactory; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.hibernate.SessionFactory] is defined: expected single matching bean but found 2: sessionFactory2,sessionFactory

}

第二次配置:

@Configuration
@EnableTransactionManagement
@PropertySource({ "classpath:hibernate.properties" })
public class RepositoryConfig2 {

@Autowired
private Environment environment;

@Bean(name="sessionFactory2")
public LocalSessionFactoryBean sessionFactory2() {
    LocalSessionFactoryBean sessionFactory2 = new LocalSessionFactoryBean();
    sessionFactory2.setDataSource(dataSource2());
    sessionFactory2.setHibernateProperties(hibernateProperties());
    sessionFactory2.setPackagesToScan(new String[] { "com.project" });
    return sessionFactory2;
}

@Bean
public Properties hibernateProperties() {
    Properties properties = new Properties();
    properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect2"));
    properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql2"));
    properties.put("hibernate.hbm2ddl.auto", "update");// environment.getRequiredProperty("hibernate.hbm2ddl.auto")
    return properties;
}

@Bean(name = "datasource2")
public DataSource dataSource2() {
    DriverManagerDataSource dataSource2 = new DriverManagerDataSource();
    dataSource2.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName2"));
    dataSource2.setUrl(environment.getRequiredProperty("jdbc.url2"));
    dataSource2.setUsername(environment.getRequiredProperty("jdbc.username2"));
    dataSource2.setPassword(environment.getRequiredProperty("jdbc.password2"));
    return dataSource2;
}


@Autowired
@Qualifier(value = "sessionFactory2")
public HibernateTransactionManager transactionManager(SessionFactory s) {
    HibernateTransactionManager txManager2 = new HibernateTransactionManager();
    txManager2.setSessionFactory(s);
    return txManager2;
}

}

1 个答案:

答案 0 :(得分:0)

好的,我做了更多的R& D,幸运的是我解决了这个问题..

所以这就是我做的事情

  1. 将我的RepositoryConfiguration合并到一个类中。
  2. 从HibernateTransactionManager中移除了自动装配并手动连接了sessionFactories。
  3. 还扫描用于扫描hibernate的cjanged包(意识到它将在两个数据库中创建相同的表)
  4. 在@Transactional中添加限定符(从spring导入。)

    @Transactional("sessionFactoryTransactionManager") 
    

    在我的存储库中。

    @Configuration
    @EnableTransactionManagement
    @PropertySource({ "classpath:hibernate.properties" })
    public class RepositoryConfiguration {
    
    @Autowired
    private Environment environment;
    
    @Bean(name = "sessionFactory")
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(dataSource());
        sessionFactory.setHibernateProperties(hibernateProperties());
        sessionFactory.setPackagesToScan(new String[] { "com.project" });
        return sessionFactory;
    }
    
    @Bean
    public Properties hibernateProperties() {
        Properties properties = new Properties();
        properties.put("hibernate.dialect",    environment.getRequiredProperty("hibernate.dialect"));
        properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
        properties.put("hibernate.hbm2ddl.auto", "update");// environment.getRequiredProperty("hibernate.hbm2ddl.auto")
    return properties;
    }
    
    @Bean(name = "sessionFactory2")
    public LocalSessionFactoryBean sessionFactory2() {
        LocalSessionFactoryBean sessionFactory2 = new  LocalSessionFactoryBean();
        sessionFactory2.setDataSource(dataSource());
        sessionFactory2.setHibernateProperties(hibernateProperties2());
        sessionFactory2.setPackagesToScan(new String[] { "com.server" });
        return sessionFactory2;
    }
    
    @Bean
    public Properties hibernateProperties2() {
        Properties properties = new Properties();
        properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect2"));
        properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql2"));
        properties.put("hibernate.hbm2ddl.auto", "update2");// environment.getRequiredProperty("hibernate.hbm2ddl.auto")
        return properties;
    }
    
    @Bean(name = "datasource2")
    public DataSource dataSource2() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName2"));
           dataSource.setUrl(environment.getRequiredProperty("jdbc.url2"));
       dataSource.setUsername(environment.getRequiredProperty("jdbc.username2"));
       dataSource.setPassword(environment.getRequiredProperty("jdbc.password2"));
        return dataSource;
    }
    
    @Bean(name = "datasource")
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
        dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
    dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
    dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
    return dataSource;
        }
    
    @Autowired
    @Qualifier("sessionFactory")
    private SessionFactory sessionFactory;
    
    @Autowired
    @Qualifier("sessionFactory2")
    private SessionFactory sessionFactory2;
    
    @Bean(name = "sessionFactoryTransactionManager")
    public HibernateTransactionManager transactionManager() {
        HibernateTransactionManager txManager = new HibernateTransactionManager();
        txManager.setSessionFactory(this.sessionFactory);
         return txManager;
    }
    
    @Bean(name = "sessionFactory2TransactionManager")
     public HibernateTransactionManager transactionManager2() {
        HibernateTransactionManager txManager2 = new HibernateTransactionManager();
        txManager2.setSessionFactory(this.sessionFactory2);
        return txManager2;
    }
    
    }