이 HibernateUtil 클래스를 사용하십시오. XML 및 특성 파일을 표준 이름없이로드합니다. 희망이 유용합니다.
public class HibernateUtil2 {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
URL r1 = HibernateUtil2.class.getResource("/hibernate2.cfg.xml");
Configuration c = new Configuration().configure(r1);
try {
InputStream is = HibernateUtil2.class.getResourceAsStream("/hibernate2.properties");
Properties props = new Properties();
props.load(is);
c.addProperties(props);
} catch (Exception e) {
LOG.error("Error al leer las propiedades", e);
}
return c.buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
LOG.error("Error al construir SessionFactory", ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}