在代码里Hibernate.initialize();可以动态的改变数据库连接地址。

解决方案 »

  1.   


    或者写两个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(); ......