1

I'm new to hibernate and I'm trying to connect to multiple DB's. I know that we can create a new cfg file separate for each DB and then create an factory of it like

factory1 = new Configuration().configure(cfg1.xml).buildSessionFactory();
factory2 = new Configuration().configure(cfg2.xml).buildSessionFactory();

But wanted to know what is the meaning of having a name like session-factory name="SESS1" in hibernate-configuration and can I use that to define multiple DB sessions there instead of defining in a new cfg file. please let me know.

j_lemon
  • 33
  • 10
pradeep
  • 53
  • 5

1 Answers1

1

if you have another database you should define the relevant configuration in hibernate.hbm.xml to create a separate SessionFactory for that database too.

Yes it is possible to do, what you need to do is change the names inside your cfg.xml file.

For example:

<bean id="dataSource1" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <!-- ... -->
</bean>

<bean id="sessionFactory1" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource1"/>
    <!-- ... -->
</bean>

<bean id="transactionManager1" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory1"/>
    <!-- ... -->
</bean>


<bean id="dataSource2" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <!-- ... -->
</bean>

<bean id="sessionFactory2" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource2"/>
    <!-- ... -->
</bean>

<bean id="transactionManager2" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory2"/>
    <!-- ... -->
</bean>

You can also check this topic here: Hibernate using multiple databases

Community
  • 1
  • 1
José Mendes
  • 852
  • 2
  • 12
  • 28