在hibernate错误中初始SessionFactory创建失败

时间:2012-09-06 08:02:54

标签: java hibernate

我正在使用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);
    }
 }
} 

1 个答案:

答案 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下(不推荐)。