休眠配置会话工厂名称 [英] hibernate-configuration session-factory name

查看:38
本文介绍了休眠配置会话工厂名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是冬眠的新手,我正在尝试连接到多个数据库.我知道我们可以为每个数据库分别创建一个新的cfg文件,然后像

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();

但是想知道在hibernate-configuration中拥有一个像session-factory name ="SESS1"这样的名称的含义,我可以用它在那里定义多个数据库会话,而不是在一个新的cfg文件中定义.请让我知道.

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.

推荐答案

如果您有另一个数据库,则应在hibernate.hbm.xml中定义相关配置以创建一个为该数据库也单独使用SessionFactory.

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

是可以的,您需要做的是更改cfg.xml文件中的名称.

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

例如:

<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

这篇关于休眠配置会话工厂名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆