hibernate-configuration session-factory name

别说谁变了你拦得住时间么 提交于 2021-01-28 21:43:56

问题


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.


回答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



来源:https://stackoverflow.com/questions/33286002/hibernate-configuration-session-factory-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!