Hibernate如何配置操作多个数据库

Hibernate如何配置操作多个数据库
 
Question:   
 
引用:   
我现在在做一个项目,需要从两个数据库实例中存取数据,在用hibernate实现的过程中该如何配置和处理   
 
--------------------------------------------------------------------------------   
 
1、如果一个项目要从两个乃至多个数据库实例读取数据,该如何处理?   
2、如果只有一个数据库oraA,为其建立建立oraA.hbm.xml以及hibernate.properties文件,该文件中的数据库连接部分写为:   
##  Oracle   
hibernate.dialect  net.sf.hibernate.dialect.OracleDialect   
hibernate.connection.driver_class  oracle.jdbc.driver.OracleDriver   
hibernate.connection.username  username   
hibernate.connection.password  password   
hibernate.connection.url  jdbc:oracle:thin:@local:1521:ora   
 
3、如有两个oracle数据库oraA和oraB,分别为其建立oraA.hbm.xml和oraB.hbm.xml映射文件,这样数据库中的各个表和实体持久化类就可以映射好,但是hibernate.properties中的数据库配置部分该如何处理呢,怎样才能同时连接上两个数据库实例呢?   
 
 
如果用这个Configuration.configure(File  configFile)   
configFile可以是不同的配置。   
 
通过配置configFile可以得到每个数据库的configuration,  也就是可以获得每个数据库实例的session;但是在hibernate.properties文件中怎么写数据库连接部分呢,一个数据库的时候写法是:hibernate.connection.url  jdbc:oracle:thin:@local:1521:ora   
 
那如果有两个数据库实例呢,在hibernate.properties文件中怎么处理,总不会是写成如下吧:   
##oracle   
hibernate.dialect  net.sf.hibernate.dialect.OracleDialect   
hibernate.connection.driver_class  oracle.jdbc.driver.OracleDriver   
hibernate.connection.username  username   
hibernate.connection.password  password   
hibernate.connection.url  jdbc:oracle:thin:@local:1521:oraA   
hibernate.connection.username  usernamet   
hibernate.connection.password  passwordt   
hibernate.connection.url  jdbc:oracle:thin:@local:1521:oraB     
 
 
Answer:   
 
写两个hibernate.properties,例如连接oraA的叫做hibernateA.properties,连接oraB的叫做hibernateB.properties。   
 
hibernateA.properties内容如下:   
 
引用:   
hibernate.dialect  net.sf.hibernate.dialect.OracleDialect   
hibernate.connection.driver_class  oracle.jdbc.driver.OracleDriver   
hibernate.connection.username  username   
hibernate.connection.password  password   
hibernate.connection.url  jdbc:oracle:thin:@local:1521:oraA     
 
 
 
hibernateB.properties内容如下:   
 
引用:   
hibernate.dialect  net.sf.hibernate.dialect.OracleDialect   
hibernate.connection.driver_class  oracle.jdbc.driver.OracleDriver   
hibernate.connection.username  usernamet   
hibernate.connection.password  passwordt   
hibernate.connection.url  jdbc:oracle:thin:@local:1521:oraB     
 
 
程序里面:   
 
代码:   
java代码:     
 
Configuration  conf_oraA  =  new  Configuration("/hibernateA.properties").addClass().......;   
 
Configuration  conf_oraB  =  new  Configuration("/hibernateB.properties").addClass().......;   
 
SessionFactory  sf_oraA  =  conf_oraA.buildSessionFactory();   
SessionFactory  sf_oraB  =  conf_oraB.buildSessionFactory();   
 
Session  s_oraA  =  sf_oraA.openSession();   
Session  s_oraB  =  sf_oraB.openSession();   
 
...... 
  
 
--------------------------------------------------------------- 
 
更正一下 
方案一: 
写两个hibernate.cfg.xml 
使用数据库a的时候 
Configuration  cfg_a=new  Configuration().configure(hibernate.cfg_a.xml); 
使用数据库b的时候 
Configuration  cfg_b=new  Configuration().configure(hibernate.cfg_b.xml);

usr/home/zk1007874/htdocs/upload_files/images


 
方案二: 
使用一个能操作两个数据库的账号,在?.hbm.xml映射文件中的class标签中指定catalog为数据库名

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值