public class HibernateUtil { private static final SessionFactory sessionFactory1; private static final SessionFactory sessionFactory2; static { try { // Create the SessionFactory from hibernate.cfg.xml sessionFactory1 = new Configuration().configure("hibernate1.cfg.xml").buildSessionFactory(); sessionFactory2 = new Configuration().configure("hibernate2.cfg.xml").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); } } public static SessionFactory getSessionFactory1() { return sessionFactory1; } public static SessionFactory getSessionFactory2() { return sessionFactory2; } }
稍微详细点,好吗?
据个例子,我不是很明白
private static final SessionFactory sessionFactory2;
static {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory1 = new Configuration().configure("hibernate1.cfg.xml").buildSessionFactory();
sessionFactory2 = new Configuration().configure("hibernate2.cfg.xml").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);
}
} public static SessionFactory getSessionFactory1() {
return sessionFactory1;
}
public static SessionFactory getSessionFactory2() {
return sessionFactory2;
}
}
但我在config("hibernate.cfg.xml")后不能找到此文件,我将hibernate.cfg.xml文件放进java source中,这是为何??
谢谢三生,可是当我使用默认时,能找到hibernate.cfg.xml
但我在config("hibernate.cfg.xml")后不能找到此文件,我将hibernate.cfg.xml文件放进java source中,这是为何??
-------------------
先用绝对路径试,把问题确定下来!