我正在使用netbeans ide并将其放置在默认的src / java文件夹中的hibernate.cfg.xml中 但每当我运行应用程序时,它会显示以下错误:
Initial SessionFactory creation failed.org.hibernate.HibernateException: /hibernate.cfg.xml not found
课程:
public class myutil {
private static final SessionFactory sessionFactory = buildsf();
public static SessionFactory buildsf() {
try {
// Create the SessionFactory from standard (hibernate.cfg.xml)
return new Configuration().configure("config/hibernate.cfg.xml").buildSessionFactory();
} catch (Throwable ex) { // Log the exception.
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
}
答案 0 :(得分:2)
使用:
SessionFactory sessionFactory = new Configuration()
.configure("config/hibernate.cfg.xml") // give path to hibernate.cfg.xml (recommended)
.buildSessionFactory();
return sessionFactory;
检查here用法。或直接将hibernate.cfg.xml
直接保留在src
下(不推荐)。