小编典典

在运行时修改hibernate.cfg.xml

hibernate

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

我的hibernate代码的一部分如下:

<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
    <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
    <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/Database</property>
    <property name="hibernate.connection.username">username</property>
    <property name="hibernate.connection.password">password</property>

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

在javaswing中,我正在实现此功能:

public static void SetSessionFactory(String url) {
    try {

  AnnotationConfiguration conf = new AnnotationConfiguration().configure();
   // <!-- Database connection settings -->
  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);
}

}

然后,我用按钮检查所做的更改,从组合框中选择所需的数据库:

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的输出未更改。不用说,我的其余代码可以访问未更改的数据库,而不是我想从运行时访问的数据库。

如何在运行时修改此值?

非常感谢!


阅读 323

收藏
2020-06-20

共1个答案

小编典典

我找到了解决问题的方法。问题是,当我想在其余的代码中使用新配​​置时,“因为每笔交易我都打开了一个新会话(如hibernate所建议),但是该会话始终是在hibernate.cfg.xml文件的开头。另外,我还在一个按钮中定义了配置功能。

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

public static void SetSessionFactory(String url, String user, String pass) {
    try {

      AnnotationConfiguration conf = new AnnotationConfiguration().configure();
      // <!-- Database connection settings -->
      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();

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

2020-06-20