首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

hibernate配备多个数据源

2012-11-20 
hibernate配置多个数据源Hibernate连接多个数据库来源:http://blog.sina.com.cn/u/1237288325?一、????????

hibernate配置多个数据源

Hibernate连接多个数据库

来源:http://blog.sina.com.cn/u/1237288325

?

一、???????????? Hibernate访问数据库时加载的过程

对于大多数使用Hibernate的朋友来说,通常使用一下方式来获得Configuration实例: Configuration configure = new Configuration().configure();

在Hibernate中,Configuration是hibernate的入口。在实例化一个Configuration的时候,Hibernate会自动在环境变量(classpath)里面查找Hibernate配置文件hibernate.properties。如果该文件存在,则将该文件的内容加载到一个Properties的实例GLOBAL_PROPERTIES里面,如果不存在,将打印信息  hibernate.properties not found;

接下来Hibernate将所有系统环境变量(System.getProperties())也添加到GLOBAL_PROPERTIES里面。如果配置文件hibernate.properties存在,系统还会进一步验证这个文件配置的有效性,对于一些已经不支持的配置参数,系统将打印出警告信息。

默认状态下configure()方法会自动在环境变量(classpath)下面寻找Hibernate配置文件hibernate.cfg.xml,如果该文件不存在,系统会打印如下信息并抛出HibernateException异常: hibernate.cfg.xml not found;如果该文件存在,configure()方法会首先访问<session-factory>,并获取该元素name的属性,如果name的属性非空,将用这个配置的值来覆盖hibernate.properties的hibernate.session_factory_name的配置的值,从这里我们可以看出,hibernate.cfg.xml里面的配置信息可以覆盖hibernate.properties的配置信息。

接下来configure()方法访问<session-factory>的子元素,首先将使用所有的<property>元素配置的信息来覆盖hibernate.properties里面对应的配置信息。

然后configure()会依次访问以下几个元素的内容

<mapping>

<jcs-class-cache>

<jcs-collection-cache>

<collection-cache>

其中<mapping>是必不可少的,必须通过配置<mapping>,configure()才能访问到我们定义的java对象和关系数据库表的映射文件(hbm.xml),例如:

<mapping resource="Cat.hbm.xml"/>

这样configure()方法利用各种资源就创建了一个Configuration实例。对于整个项目来说,如果用一个本地线程来存放这个Configuration实例,那么整个项目只需要实例化一次Configuration对象(注:Configuration实例很花费时间),也就提高了项目的效率。

?

二、???????????? Hibernate访问多个数据库的配置

根据以上所述,configure()方法默认是通过访问hibernate.cfg.xml的<mapping>元素来加载我们提供的.hbm.xml文件。我们也可以直接指定hbm.xml文件,例如addClass()方法可以直接通过指定class来加载对应的映射文件,hibernate会将提供的class的全名(包括package)自动转化为文件路径,还可以用addFile方法直接指定映射文件。例如:Configuration configurate = new Configuration().addClass(“Book.class”);

Configuration configurate = new Configuration().addURL(Configuration.class.getResource ("/Book.hbm.xml"));

Configuration config = new Configuration().addFile("/Cat.hbm.xml");

这样,如果用xml配置来配置多个数据库的话,那就写多个配置文件。这里假设对应两个数据库(一个是MySQL,一个是SQLServer),我们可以把其xml文件定义为“mysql.cfg.xml”和“sqlserver.cfg.xml”。则用Configuration类获取SessionFactory的代码如下:

SessionFactory mysqlFactory = new Configuration().configure("/mysql.cfg.xml").buildSessionFactory();

SessionFactory sqlserverFactory = new Configuration().configure("sqlserver.cfg.xml ").buildSessionFactory();

同时连接两个数据库实例:

问题:连接mssql中的vedio 和bbs数据库

解决方案:

1.vedio.cfg.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
?"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
?"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory name="bbs">
?<!-- 指定连接数据库的用户名 -->
?<property name="connection.username">sa</property>
?<!-- 指定连接数据库的URL -->
?<property name="connection.url">
??jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=vedio;??
?</property>

?<!-- 指定数据库使用的SQL方言 -->
?<property name="dialect">
??org.hibernate.dialect.SQLServerDialect
?</property>
?<!-- 指定连接数据库的口令 -->
?<property name="connection.password">sa</property>
?<!-- 指定连接数据库的驱动程序 -->
?<property name="connection.driver_class">
??com.microsoft.jdbc.sqlserver.SQLServerDriver
?</property>
?<!-- true表示程序运行时,在控制台显示SQL语句,false表示不显示SQL语句 -->
?<property name="show_sql">true</property>
?<!-- <property name="connection.pool_size">50</property> -->

?<!-- 持久化类所对应的映射文件 -->
?<mapping resource="vedio/Managers.hbm.xml" />


</session-factory>
</hibernate-configuration>

?

2. bbs.cfg.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
?"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
?"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory name="bbs">
?<!-- 指定连接数据库的用户名 -->
?<property name="connection.username">sa</property>
?<!-- 指定连接数据库的URL -->
?<property name="connection.url">
??jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=bbs;??
?</property>
?<!-- 指定数据库使用的SQL方言 -->
?<property name="dialect">
??org.hibernate.dialect.SQLServerDialect
?</property>
?<!-- 指定连接数据库的口令 -->
?<property name="connection.password">sa</property>
?<!-- 指定连接数据库的驱动程序 -->
?<property name="connection.driver_class">
??com.microsoft.jdbc.sqlserver.SQLServerDriver
?</property>
?<!-- true表示程序运行时,在控制台显示SQL语句,false表示不显示SQL语句 -->
?<property name="show_sql">true</property>
?<!-- <property name="connection.pool_size">50</property> -->

?<!-- 持久化类所对应的映射文件 -->
?<mapping resource="bbs/Board.hbm.xml" />
?<mapping resource="bbs/BBSUser.hbm.xml" />
?<mapping resource="bbs/BBSManager.hbm.xml" />
?<mapping resource="bbs/Image.hbm.xml" />
?<mapping resource="bbs/Upfile.hbm.xml" />
?<mapping resource="bbs/Post.hbm.xml" />
?<mapping resource="bbs/BestPost.hbm.xml" />
?<mapping resource="bbs/User.hbm.xml" />
?<mapping resource="bbs/Groups.hbm.xml" />
?<mapping resource="bbs/Help.hbm.xml" />
?<mapping resource="bbs/Note.hbm.xml" />
?<mapping resource="vedio/Managers.hbm.xml" />


</session-factory>
</hibernate-configuration>

3.VedioHibernateUTL.java

package vedio;

import org.hibernate.*;
import org.hibernate.cfg.*;

public class HibernateUtil {
?
?public static final SessionFactory sessionFactory;
??static {
???try{
????sessionFactory=new Configuration().configure("/vedio.cfg.xml").buildSessionFactory();
????
???}catch(Throwable ex) {
????
????throw new ExceptionInInitializerError(ex);
???}
??}
?
?public static final ThreadLocal<Session> session =new ThreadLocal<Session>();
?
?//create session
?public static Session currentSession() throws HibernateException {
??Session s= session.get();
??if(s==null||!s.isOpen()) {
???s=sessionFactory.openSession();
???session.set(s);
??}
??return s;
?}
?
?//close session
?public static void closeSession() throws HibernateException {
??Session s=session.get();
??session.set(null);
??if(s!=null) {
???s.close();
??}
??
?}
}

?

4.BbsHibernateUTL.java

package bbs;

import org.hibernate.*;
import org.hibernate.cfg.*;

public class HibernateUtil {
?
?public static final SessionFactory sessionFactory;
??static {
???try{
????sessionFactory=new Configuration().configure("/bbs.cfg.xml").buildSessionFactory();
????
???}catch(Throwable ex) {
????
????throw new ExceptionInInitializerError(ex);
???}
??}
?
?public static final ThreadLocal<Session> session =new ThreadLocal<Session>();
?
?//create session
?public static Session currentSession() throws HibernateException {
??Session s= session.get();
??if(s==null||!s.isOpen()) {
???s=sessionFactory.openSession();
???session.set(s);
??}
??return s;
?}
?
?//close session
?public static void closeSession() throws HibernateException {
??Session s=session.get();
??session.set(null);
??if(s!=null) {
???s.close();
??}
??
?}
}

然后写相应的 表名.hbm.xml文件

总结一下:一人数据源对应一个.cfg.xml文件,一个HibernateUTL.java,对应多个 表名.hbm.xml

?

如果你用spring,多数据库就更简单了,像这段代码可以完成所有配置:

<beans>

<bean id="mysqlDS" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<property name="dataSource">

<ref local="sqlserverDS"/>

</property>

<property name="hibernateProperties">

<props>

<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>

</props>

</property>

<property name="mappingResources">

<list>

<value>test.hbm.xml</value>

</list>

</property>

</bean>

.......

</beans>

热点排行