天天看点

Hibernate异常之org.hibernate.HibernateException: /hibernate.cfg.xml not found

1、异常描述

org.hibernate.HibernateException: /hibernate.cfg.xml not found

备注:

      此处hibernate版本号:3.6.10.Final

2、分析原因

一下是hibernate创建session工厂的示例代码:

private static Configuration configure;
	private static SessionFactory sessionFactory;
	
	static{
	    try {
                   // 实例化配置对象
		   configure = new Configuration().configure();
                   // 利用配置对象创建session工厂
		   sessionFactory = configure.buildSessionFactory();
           } catch (HibernateException ex) {
                throw new ExceptionInInitializerError(ex);
           }
	}
           

在以上示例代码中,当实例化配置对象时,hibernate框架会查找一个名为  hibernate.cfg.xml 的配置文件。如果找不到该配置文件,就会报此错

3、解决方式

情况1:查看hibernate的配置文件的文件名是否出错

情况2:查看hibernate.cfg.xml放置的位置是否正确

            如果是普通的java项目(java web项目),hibernate.cfg.xml必须放置的需要引用它的类的外层()推荐直接放置在src根目录

            如果是maven项目,hibernate.cfg.xml直接放在 src/main/resource 目录下即可

此文希望可以帮助到大家。如有错误,请指教。                                                           

如果大家还有其他的情况或者好的解决方法,也望指教,感谢阅读。 

继续阅读