怎样用Hibernate处理两个数据库

xupeng19705 2007-12-03 07:59:33
用struts和hibernate架构做一个小项目,项目中有两个数据库,但不知道怎样配置 hibernate.cfg.xml 和 hibernateUtil.java 等相关文件,Hibernate中一个SessionFactory就是对应一个数据库的配置,所以需要有两个SessionFactory,两个怎么配置呢?
希望大家帮个指点一下,谢谢!
...全文
135 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
xupeng19705 2007-12-05
  • 打赏
  • 举报
回复
Configuration cfg_a=new Configuration().configure(hibernate.cfg_a.xml);
中不能直接使用 ibernate.cfg_a.xml 作为参数,我看过关于 configure() 函数的定义,
public org.hibernate.cfg.Configuration configure() throws org.hibernate.HibernateException;
public org.hibernate.cfg.Configuration configure(java.lang.String resource) throws org.hibernate.HibernateException;
public org.hibernate.cfg.Configuration configure(java.net.URL url) throws org.hibernate.HibernateException;
public org.hibernate.cfg.Configuration configure(java.io.File configFile) throws org.hibernate.HibernateException;
public org.hibernate.cfg.Configuration configure(org.w3c.dom.Document doc) throws org.hibernate.HibernateException;

我尝试过第二种,直接在 ibernate.cfg_a.xml 上加了双引号,即
Configuration cfg_a=new Configuration().configure("hibernate.cfg_a.xml");
好像也通不过,???
其它几种倒是不会用,不知道怎么定义?
天外流星 2007-12-04
  • 打赏
  • 举报
回复
方案一:

写两个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);
zhouyueyue 2007-12-04
  • 打赏
  • 举报
回复
写两个配置文件吧。。。
不太清楚。。
xupeng19705 2007-12-04
  • 打赏
  • 举报
回复
你讲得太模糊了,能否发个相关的实例呀,
我的邮箱:xupeng19705@163.com
我非常感谢你的参加,多谢了
mingr6370 2007-12-03
  • 打赏
  • 举报
回复
factory

试试吧,如果是多个库,用配置文件
Jinxp 2007-12-03
  • 打赏
  • 举报
回复
关注中......

58,454

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧