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!