hibernate.cfg.xml在运行时修改

我有一个问题,有些人已经解决了,但问题是我不明白我的实现中缺少什么。

我的部分hibernate代码如下:

  org.hibernate.dialect.PostgreSQLDialect org.postgresql.Driver jdbc:postgresql://localhost:5432/Database username password 

问题是我想通过更改hibernate.connection.url属性中的“database”字来选择我想在运行时使用的数据库。

在javaswing,我正在实现这个function:

 public static void SetSessionFactory(String url) { try { AnnotationConfiguration conf = new AnnotationConfiguration().configure(); //  conf.setProperty("hibernate.connection.url", url); SessionFactory SESSION_FACTORY = conf.buildSessionFactory(); //DEBUG1=With this output I intend to check if the parameter has changed System.out.println("Connection changed to " + conf.getProperty("hibernate.connection.url")); } catch (Throwable ex) { // Log exception! throw new ExceptionInInitializerError(ex); } 

}

然后,我检查用按钮进行的​​更改,从combobox中选择我想要的数据库:

 private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: String url; int areaIndex = this.areaComboBox.getSelectedIndex(); switch (areaIndex) { case 0: url="jdbc:postgresql://localhost:5432/Database"; break; case 1: url="jdbc:postgresql://localhost:5432/Database1"; break; case 2: url="jdbc:postgresql://localhost:5432/Database2"; break; default: url="jdbc:postgresql://localhost:5432/Database"; break; } SetSessionFactory(url); AnnotationConfiguration config = new AnnotationConfiguration().configure(); //DEBUG2= With this output I want to confirm the changes of the property outside the setSessionFactory function System.out.println("DATABASE= " + config.getProperty("hibernate.connection.url")); } 

现在,debug1的输出正在改变,所以我在这个打印中获得了我想要的数据库的名称,但是debug2的输出没有改变。 不用说,我的其余代码可以访问未更改的数据库,而不是我想从运行时访问的数据库。

如何在运行时修改此值?

非常感谢!

我找到了解决问题的方法。 问题是,当我想在其余代码中使用新配​​置时,我不能,’因为每个事务我打开了一个新会话(按照hibernate的建议),但那个会话始终是那个会话。在hibernate.cfg.xml文件的开头。 另外,我在一个按钮中定义了我的配置function。

现在我改变了我的函数的位置并将其放在HibernateUtil.java中,只添加了我需要的配置以及稍后可能有用的配置

 public static void SetSessionFactory(String url, String user, String pass) { try { AnnotationConfiguration conf = new AnnotationConfiguration().configure(); //  conf.setProperty("hibernate.connection.url", url); conf.setProperty("hibernate.connection.username", user); conf.setProperty("hibernate.connection.password", pass); sessionFactory = conf.buildSessionFactory(); } catch (Throwable ex) { // Log exception! throw new ExceptionInInitializerError(ex); } } 

然后,任何时候我想访问那个新连接,在每个事务开始时我调用会话指向同一个类HibernateUtil.java

 public Session session = HibernateUtil.getSessionFactory().openSession(); 

如果不将第一个函数放在此类中,则打开的会话始终是配置文件中默认的会话。

试试这个(将表A的登录字段复制到数据库main id 1,复制到表C的电子邮件字段,数据库foregin id为1):

 // config SessionFactory mainSF = new Configuration() .addResource("A.hbm.xml") .addResource("B.hbm.xml") .setProperty("dialect", "org.hibernate.dialect.PostgreSQLDialect") .setProperty("connection.url", "jdbc:postgresql://your.first.db/main") .setProperty("connection.username","username") .setProperty("connection.password","password").buildSessionFactory(); SessionFactory foreginSF = new Configuration() .addResource("C.hbm.xml") .addResource("D.hbm.xml") .setProperty("dialect", "org.hibernate.dialect.PostgreSQLDialect") .setProperty("connection.url", "jdbc:postgresql://your.second.db/foregin") .setProperty("connection.username","username") .setProperty("connection.password","password").buildSessionFactory(); // pre business Session main = mainSF.openSession(); Session foregin = foreginSF.openSession(); // now your buissness, ie: A a = (A) main.get(A.class, 1); C c = (C) foregin.get(C.class, 1); Transaction foreginTransaction = foregin.beginTransaction(); c.setEmail(a.getLogin()); foregin.saveOrUpdate(c); foreginTransaction.commit(); // post business main.close(); foregin.close();