2012-09-06 67 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

非常感谢你的兄弟 –