Java org.hibernate.HibernateException:/hibernate.cfg.xml未找到

Java org.hibernate.HibernateException:/hibernate.cfg.xml未找到,java,hibernate,spring-mvc,Java,Hibernate,Spring Mvc,我正试图将hibernate与Spring3MVC结合使用,但目前抛出了这个异常。我想我需要在某个地方定义我的hibernate.cfg.xml,但不确定在哪里 我在这里基本上遵循了这个示例,特别是在那里看到了这行代码,它们假设使用以下代码“神奇地”找到我的hibernate.cfg文件: return new Configuration().configure().buildSessionFactory(); 我猜这是不对的?我目前在src/com/jr/hibernate/ 下面是我的cf

我正试图将hibernate与Spring3MVC结合使用,但目前抛出了这个异常。我想我需要在某个地方定义我的
hibernate.cfg.xml
,但不确定在哪里

我在这里基本上遵循了这个示例,特别是在那里看到了这行代码,它们假设使用以下代码“神奇地”找到我的hibernate.cfg文件:

return new Configuration().configure().buildSessionFactory();
我猜这是不对的?我目前在
src/com/jr/hibernate/

下面是我的cfg文件:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
  <session-factory>
    <!-- Database connection settings -->
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/racingleague</property>
    <property name="connection.username">username</property>
    <property name="connection.password">password</property>
    <property name="hibernate.format_sql">true</property>
    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property>
    <!-- SQL dialect -->
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <!-- Enable Hibernate's automatic session context management -->
    <property name="current_session_context_class">thread</property>
    <!-- Disable the second-level cache  -->
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    <!-- Echo all executed SQL to stdout -->
    <property name="hibernate.show_sql">true</property>
    <!-- Drop and re-create the database schema on startup -->
    <property name="hibernate.hbm2ddl.auto">update</property>
    <!--property name="hbm2ddl.auto">update</property-->
    <mapping resource="com/jr/model/hibernateMappings/user.hbm.xml"/>
  </session-factory>
</hibernate-configuration>
它被称为这个抽象类:

package com.jr.db;

import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;

import com.jr.utils.HibernateUtils;

public abstract class DbWrapper<T> {

    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表hibernate xml映射与我的hibernate.cfg.xml文件位于同一位置,启动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(..)
方法从自定义位置加载它。

hibernate.cfg.xml
文件放置在
src/com/jr/hibernate/
目录下,而不是将其放置在
src
目录下。然后,它将自动出现在这里的人们提到的
WEB-INF/classes
目录中。

在IntelliJ中,转到“打开项目设置”>>Modules>>Hibernate,并以项目中使用的Hibernate.cfg.xml文件为目标。

我遇到了同样的问题,并将Hibernate.cfg.xml移到src/main/resources目录中,它将自动放置在/WEB-INF/classes中。

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

SessionFactory工厂=新工厂 Configuration().configure(“hibernate.cfg.xml”).addAnnotatedClass().buildSessionFactory()


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

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

运行
mvn后,请清洁安装
。使用Try-and-error,我可以通过从
src
中删除
hibernate.cfg.xml
并将其添加到其他地方来解决这个问题。运行应用程序(在我的例子中,它是一个主类)。在这期间,我仍然得到错误。并将其添加回
src
文件夹,然后返回主类<代码>成功了

干杯。它起作用了。我还有一个问题。我的应用程序似乎找不到我的hibernate xml映射。我把它放在com.jr.hibernateMappings中,但是build.xml似乎没有构建和编译我的hbm.xml文件。是不是又放错地方了?它是否需要位于WAR/WEB-INF/classes文件夹中?如果可能的话,我更希望它位于src目录中。在将我的文件(hibernate.cfg.xml)移动到src后,它会找到该文件,非常感谢againbozzo,
config.configure(“/resources/hibernate.cfg.xml”)对我不起作用。实际上,我正在做一个简单的
hibernate3.1
app。顺便说一句,
config
属于
AnnotationConfiguration
.Thnx类型。在这之后我几乎浪费了2天时间。那么NetBeans呢?
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";
    }

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