2014-01-15 46 views
0

在我的Spring Java Config文件中,我成功使用AbstractRoutingDatasource來切換多個數據庫連接。AbstractRoutingDatasource和hibernate.dialect具有多個數據源

public MyRoutingDataSource myRoutingDataSource() { 
    MyRoutingDataSource dataSource = new MyRoutingDataSource(); 
    Map<Object, Object> targetDataSources = new HashMap<Object, Object>(); 
    dataSource.setDefaultTargetDataSource(defaultDataSource()); 
    dataSource.setTargetDataSources(targetDataSources); 
    return dataSource; 
} 

但是,當我想從數據源的PostgreSQL /甲骨文等。(如分頁限制/ rowNums)不同,具體的SQL請求,我不得不相關的特定休眠方言

HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); 
    vendorAdapter.setGenerateDdl(false); 
    vendorAdapter.setShowSql(true); 
    vendorAdapter.setDatabasePlatform("org.hibernate.dialect.PostgreSQL81Dialect"); 

    LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); 
    factory.setJpaVendorAdapter(vendorAdapter); 
    factory.setPackagesToScan("fr.appli.model"); 
    factory.setDataSource(myRoutingDataSource()); 

我的問題是如何改變我的EntityManager冬眠方言,當我從AbstractRoutingDatasource改變數據源?是否有可能以編程方式執行?

感謝

+0

能否請您詳細闡述這一點?你解決了這個問題嗎?我很努力使這種方式與各種方言,但無法找到解決方案。 – Mejmo

回答

0

我認爲要實現這一目標,你將需要多個數據源配置文件和每個數據庫都有自己的SessionFactory對象在DAO將permof DB操作。

例如:

SessionFactory sessionFactory1 = new Configuration().configure("oracleconfig.cfg.xml").buildSessionFactory(); 

SessionFactory sessionFactory2 = new Configuration().configure("mysqlconfig.cfg.xml").buildSessionFactory(); 
+0

是的,使用DAO可以配置多個數據源條件,以便擁有許多'SessionFactory's。 – Amogh

+0

請更新您的狀態/反饋? – Amogh

相關問題