私はHibernateには本当に新しく、しばらく何かを悩まされています。 Tomcatのcontext.xmlにJNDI接続文字列が定義されている2つのデータベースがあります。スプリングを使用して自分のアプリケーションで、私は次のように最初のものである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>
第二のデータベースへの第2の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();
私は常にデフォルトで最初のファクトリを取得し、スキーマ内のすべてのテーブルにアクセスできますが、2番目のテーブルにはアクセスできません。どの工場を使用したいのですか?
こんにちはJB、返信ありがとうございます。私はgetSessionFactory()を呼び出すSpringの 'getHibernateTemplate()'メソッドを使用しています。しかし、どちらも私が上記の元の投稿に含まれているhibernate.cfgで定義したsessionFactoryのIDを指定できる引数を受け入れません。 私はHibernateTemplateを直接使用しようとしましたが、テンプレートを返すセッションファクトリを指定する方法はまだ見つかりません。 SessionFactory.getCurrentSession()はそのファイルのデフォルトセッションを返しませんか? – Cranialsurge
どのオブジェクト/タイプでgetHibernateTemplate()を呼び出しますか? getCurrentSession()はインスタンスメソッドです。オブジェクトに定義した2つのセッションファクトリをインジェクトし、ターゲットにするデータベースに応じて、どちらか一方でgetSessionFactoryを呼び出すことができます。 –