2012-09-06 62 views
3

firends IM使用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

用途:

SessionFactory sessionFactory = new Configuration() 
     .configure("config/hibernate.cfg.xml") // give path to hibernate.cfg.xml (recommended) 
     .buildSessionFactory(); 

     return sessionFactory; 

檢查here的用法。或直接保留hibernate.cfg.xml直接在src(不推薦)。

+0

config目錄我需要創建嗎? –

+0

好吧,保持單獨的包/目錄配置。你也可以在那裏給你的路。 –

+0

非常感謝你的兄弟 –