运行时修改hibernate.cfg.xml [英] hibernate.cfg.xml modification on runtime
问题描述
我有这个问题,有些人已经解决了,但问题是我不明白我的实现中缺少什么。
部分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>用户名< / property>
< property name =hibernate.connection.password>密码< / property>
事情是我想选择我想在运行时使用的数据库,方法是更改hibernate.connection.url属性中的数据库字。
在javaswing中,我正在实现此功能:
public static void SetSessionFactory(String url){
try {
AnnotationConfiguration conf = new AnnotationConfiguration()。configure();
//<! - 数据库连接设置 - >
conf.setProperty(hibernate.connection.url,url);
SessionFactory SESSION_FACTORY = conf.buildSessionFactory();
// DEBUG1 =有了这个输出,我打算检查参数是否已经改变
System.out.println(Connection changed to+ conf.getProperty(hibernate.connection.url ));
} catch(Throwable ex){
//记录异常!
抛出新的ExceptionInInitializerError(ex);
}
}
然后,我检查使用按钮所做的更改,从组合框中选择我想要的数据库:
private void jButton2ActionPerformed(java .awt.event.ActionEvent evt){
// TODO在这里添加您的处理代码:
String url;
int areaIndex = this.areaComboBox.getSelectedIndex();
switch(areaIndex){
case 0:
url =jdbc:postgresql:// localhost:5432 / Database;
休息;
案例1:
url =jdbc:postgresql:// localhost:5432 / Database1;
休息;
案例2:
url =jdbc:postgresql:// localhost:5432 / Database2;
休息;
默认值:
url =jdbc:postgresql:// localhost:5432 / Database;
休息;
}
SetSessionFactory(url);
AnnotationConfiguration config = new AnnotationConfiguration()。configure();
// DEBUG2 =有了这个输出,我想确认setSessionFactory函数之外的属性更改
System.out.println(DATABASE =+ config.getProperty(hibernate.connection.url ));
$ / code>
现在,debug1的输出正在改变,所以我得到这个print我想要的数据库的名称,但debug2的输出没有改变。不用说,我的代码的其余部分可以访问未更改的数据库,而不是我想要从运行时访问的数据库。
如何修改此在运行时的值?
非常感谢!
I找到了解决我的问题的方法。问题是,当我想在其余代码中使用新配置时,我无法做到这一点,因为每次事务我都打开一个新会话(按照hibernate的建议),但那个会话总是那个在hibernate.cfg.xml文件的开头。另外,我在一个按钮中定义了我的配置功能。
现在我改变了我的函数的位置并将它放在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){
//记录异常!
抛出新的ExceptionInInitializerError(ex);
}
}
然后,任何时候我想访问那个新的连接,在我调用会话的每个事务开始时指向同一个类HibernateUtil.java
public Session session = 。HibernateUtil.getSessionFactory()的openSession();
没有在这个类中放置第一个函数,正在打开的会话总是那个默认情况下在配置文件中。
I have this question that some people have already solved, but the thing is I don't understand what is missing in my implementation.
Part of my hibernate code is as follows:
<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>
the thing is that I want to select the database that I want to use in the runtime, by changing the "database" word in the hibernate.connection.url property.
In javaswing, I'm implementing this function:
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);
}
}
Then, I check the changes made with a button, selecting the database that I want from a 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"));
}
Now, the output of the debug1 is changing, so I get in this print the name of the database that I want, but the output of the debug2 is not changing. Needless to say that the rest of my code have access to the unchanged database, not the one that I want to access from the runtime.
How can I get to modify this value on the runtime?
Thanks a lot!
I found a way to solve my issue. The thing is that when I wanted to use the new configuration in the rest of the code I couldn't, 'cause for each transaction I opened a new session (as recommended by hibernate), but the that session was always the one that was at the beginning of the hibernate.cfg.xml file. Also, I was defining my configuration function in one button.
Now I changed the position of my function and put it in HibernateUtil.java, adding just the configurations that I needed and some that could be useful later
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);
}
}
And then, any time that I want to access to that new connection, at the beginning of each transaction I call the session pointed to the same class HibernateUtil.java
public Session session = HibernateUtil.getSessionFactory().openSession();
Without putting the first function in this class, the session that was being opened was always the one that was by default in the configuration file.
这篇关于运行时修改hibernate.cfg.xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!