如题,配置多个sessionfactory 但我也只能一次操作一个数据库, 想一个sql语句联查多个数据库呢?可以实现无缝切换么?比如说我有个customer表,然后customer表在数据库A。还有个address表在B,这个时候我在DAO中的一个SQL我想做这2个表的JOIN怎么办?我只是注入了一个session,通过spring.
当然我配置了2个datasource,对应了2个sessionFactory.但是问题在于要做两个不同数据库的表的连接操作,一个sessionFactory无法完成。这个时候该怎么办?
如果不能那么Hibernate也无法做到,
表的连接操作????
来个Oracle及SqlServer??
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);
方案二:
使用一个能操作两个数据库的账号,在?.hbm.xml映射文件中的class标签中指定catalog为数据库名http://developer.51cto.com/art/200907/133239.htm
13楼的是配了2个datasource 2个sessionfactory 但是同样没有办法实现1条语句里面查询2个库的操作
楼主在3楼说过sybase是可以实现 数据库名.表名.属性名的查找 只有这样才能实现我个人的想法
首先,在一条hql中用到2个datasource应该是不行的 因为一个hql对应着一个sessionfactory,一个sessionfactory对应着一个datasource。所以hql的方法应该是行不通的。
那只能是纯sql的方法,通过jdbc的原始方法吧 具体方法我不清楚 还请高手 赐教 期待 学习
楼主要是搞出来了一定要告诉大家。这个问题一般真还不容易遇到