Hello together!
how can I use a Application with hibernate and different Datasource????
e.g. Also test.hibernate.cfg.xml
db_start.hibernate.cfg.xml replaced
Code:
....
protected static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
SessionFactory sf = null;
System.out.println("DBHandler.url " + DBHandler.url);
Configuration cfg = new Configuration();
if (DBHandler.url.equals("jdbc:mysql://10.100.1.10/test")){
cfg.configure("/test.hibernate.cfg.xml");
cfg.buildSettings();
} else if (DBHandler.url.equals("jdbc:mysql://10.100.1.10:3306/work")){
cfg.configure("/work.hibernate.cfg.xml");
else {
cfg.configure();
}
return cfg.buildSessionFactory();
}
catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
....
}
Code:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.driver_clas">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">pw123</property>
<property name="hibernate.connection.url">jdbc:mysql://10.100.1.10:3306/db_start</property>
<property name="hibernate.connection.username">user</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<mapping resource="de/wantech/mda/ZahlungBelegkopf.hbm.xml" />
<mapping resource="de/wantech/mda/ReportVerleihStatistik.hbm.xml" />
.....
It is neccasary to build for each DB a own application???