2011-07-08 29 views
1

對於Hibernate我真的很陌生,並且一直在研究一些東西。我有兩個數據庫,我在Tomcat的context.xml中定義了JNDI連接字符串。在我的應用程序,它使用了Spring和Hibernate我有2個會議工廠,第一個是如下 - >如何在Spring和Hibernate中使用會話工廠處理多個數據庫連接

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
<property name="hibernateProperties"> 
    <props> 
    <prop key="hibernate.hbm2ddl.auto">update</prop> 
    <prop key="hibernate.connection.pool_size">10</prop> 
    <prop key="hibernate.show_sql">true</prop> 
    <prop key="hibernate.transaction.auto_close_session">true</prop> 
    <prop key="hibernate.transaction.flush_before_completion">true</prop> 
    <prop key="current_session_context_class">true</prop> 

    <!--HSQL--> 
    <prop key="hibernate.connection.datasource">java:comp/env/jdbc/xxx</prop> 
    <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop> 
    <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop> 

    </props> 
</property> 
<property name="annotatedClasses"> 
    <list> 
    <value>com.mytest.examples.Person</value> 
    <value>com.mytest.examples.Customer</value> 
    <value>com.mytest.examples.Employee</value> 
    </list> 
</property> 

第二SessionFactory的點到第二個數據庫如下

<bean id="sessionFactory2" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
<property name="hibernateProperties"> 
    <props> 
    <prop key="hibernate.hbm2ddl.auto">update</prop> 
    <prop key="hibernate.connection.pool_size">10</prop> 
    <prop key="hibernate.show_sql">true</prop> 
    <prop key="hibernate.transaction.auto_close_session">true</prop> 
    <prop key="hibernate.transaction.flush_before_completion">true</prop> 
    <prop key="current_session_context_class">true</prop> 

    <!--HSQL--> 
    <prop key="hibernate.connection.datasource">java:comp/env/jdbc/yzz</prop> 
    <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop> 
    <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop> 

    </props> 
</property> 
<property name="annotatedClasses"> 
    <list> 
    <value>com.mytest.examples.Container</value> 
    <value>com.mytest.examples.Credentials</value> 
    </list> 
</property> 

現在,當我嘗試使用這些工廠使用以下

Session session = getHibernateTemplate().getSessionFactory().openSession(); 

我總是默認獲得第一個工廠,並且能夠訪問其架構中的所有表,但不能訪問第二個工廠。如何指定我想使用的工廠?

回答

1

HibernateTemplate構造函數以SessionFactory作爲參數。我不知道getHibernateTemplate()在你的代碼中做了什麼,但是它應該返回一個HibernateTemplate,它由你定義的SessionFactory bean之一構建(可以通過在spring上下文中聲明它們xml文件,也可以通過Java中的一個注入會話工廠)。

需要注意的是,作爲HibernateTemplate的的文件說,(粗體):

由於休眠3.0.1,事務 的Hibernate訪問代碼也可以 編碼在使用Hibernate的風格。因此,對於新開始的項目, ,考慮 採用編碼數據訪問對象的標準Hibernate3風格 而不是 基於 SessionFactory.getCurrentSession()。

我會直接注入會話工廠,並直接使用Hibernate API。 HibernateTemplate並沒有帶來很多Hibernate API,並且經常妨礙,恕我直言。 (例如通過不提供與Query.uniqueResult()相當的值)。

+0

嗨JB,謝謝你的回覆。我正在使用Spring的'getHibernateTemplate()'方法來調用getSessionFactory()。然而,他們都不接受一個參數,我可以指定我想要使用的sessionFactory的ID,這是我在上面的原始文章中包含的hibernate.cfg中定義的。 我試過直接使用HibernateTemplate,但我仍然沒有找到一種方法來指定我想要模板返回的sessionFactory。 會不會SessionFactory.getCurrentSession()返回該文件中的默認會話? – Cranialsurge

+1

你調用哪個對象/類型getHibernateTemplate()?至於getCurrentSession():這是一個實例方法。您可以注入在對象中定義的兩個會話工廠,並根據要定位的數據庫調用getSessionFactory中的一個或另一個。 –

相關問題