org.hibernate.HibernateException:找不到/hibernate.cfg.xml

我正在尝试使用spring 3 mvc的hibernate但是此刻我抛出了这个exception。 我想我需要在某处定义我的hibernate.cfg.xml ,但不确定在哪里?

我基本上按照这个例子http://www.nabeelalimemon.com/blog/2010/05/spring-3-integrated-with-hibernate-part-a/特别是看到这行代码,假设“神奇地” “使用这个找到我的hibernate.cfg文件:

 return new Configuration().configure().buildSessionFactory(); 

我猜这不对吗? 我目前在src/com/jr/hibernate/有我的hibernate.cfg文件

下面是我的cfg文件:

      com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/racingleague username password true  1  org.hibernate.dialect.MySQLDialect  thread  org.hibernate.cache.NoCacheProvider  true  update     

我的hibernate utils类:

 package com.jr.utils; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtils { private static final SessionFactory sessionFactory = buildSessionFactory(); public static SessionFactory buildSessionFactory() { try { // Create the SessionFactory from hibernate.cfg.xml return new Configuration().configure().buildSessionFactory(); } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } } 

这个抽象类叫做bu:

 package com.jr.db; import org.hibernate.SessionFactory; import org.hibernate.classic.Session; import com.jr.utils.HibernateUtils; public abstract class DbWrapper { private static SessionFactory sessionFactory = null; private static Session session; public DbWrapper() { setSessionFactory(); } private void setSessionFactory() { sessionFactory = HibernateUtils.buildSessionFactory(); session = sessionFactory.getCurrentSession(); } public boolean addNewItem(T dbItem) { try { session.getTransaction().begin(); session.save(dbItem); session.getTransaction().commit(); } catch (Exception e) { System.err.println("error exception when adding new item to table" + e); } finally { session.close(); sessionFactory.close(); } return false; } public abstract boolean removeItem(String uid); public abstract boolean modifyItem(String uid, T item); } 

这是控制器最初做一些hibernate的东西:

 private Logger logger = Logger.getLogger(UserController.class); private UserDb userDb; @RequestMapping(value = "/user/registerSuccess", method = RequestMethod.POST) public String submitRegisterForm(@Valid User user, BindingResult result) { // validate the data recieved from user logger.info("validate the data recieved from user"); if (result.hasErrors()) { logger.info("form has "+result.getErrorCount()+" errors"); return "account/createForm"; } else{ // if everthings ok, add user details to database logger.info("if everthings ok, add user details to database"); userDb = new UserDb(); userDb.addNewItem(user); // display success and auto log the user to the system. return "account/main"; } } 

提前干杯。 我还将所有表格的hibvernate xml映射都放在与hibernate.cfg.xml文件相同的位置

而不是将hibernate.cfg.xml文件放在src/com/jr/hibernate/目录下,而是将它放在src目录下。 然后它将自动出现在WEB-INF/classes目录中,如此处的人员所述。

启动webapp时,必须在类路径的根目录中找到hibernate.cfg.xml

如果使用maven构建项目,请将hibernate.cfg.xml放在src/main/resources目录中,这样在构建war包时,它将自动放在/WEB-INF/classes

如果不使用maven,请将文件直接放在WEB-INF/classes目录中。

hibernate.cfg.xml应该在WEB-INF/classes 。 或者,您可以通过将相应的参数传递给configure(..)方法从自定义位置加载它。

如果您使用Maven,则应将文件hibernate.cfg.xml放在Intellij IDEA的以下路径/src/main/java/resources/hibernate.cfg.xml中。 然后,在您运行的应用程序类中,只需插入行:

SessionFactory factory = new Configuration()。configure(“hibernate.cfg.xml”)。addAnnotatedClass()。buildSessionFactory();

在IntelliJ中,转到“打开项目设置”>>模块>> Hibernate并定位项目中使用的hibernate.cfg.xml文件。

我有同样的问题,并将hibernate.cfg.xml移动到src / main / resources目录解决,它将自动放在/ WEB-INF / classes中。

即使我在src文件夹下有hibernate.cfg.xml ,我也明白了

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

运行mvn clean install 。 使用Try和error我可以通过从src删除hibernate.cfg.xml来解决它并添加到其他地方。 运行应用程序(在我的情况下,它是一个主类)。 在此期间我仍然得到错误。 并将其添加回src文件夹并朗读主类。 It worked!