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

jon*_*ney 10 java hibernate spring-mvc

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

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

return new Configuration().configure().buildSessionFactory();
Run Code Online (Sandbox Code Playgroud)

我猜这不对吗?我目前有我的hibernate.cfg文件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>
Run Code Online (Sandbox Code Playgroud)

我的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);
        }
      }

}
Run Code Online (Sandbox Code Playgroud)

这个抽象类被称为bu:

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);

}
Run Code Online (Sandbox Code Playgroud)

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

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";
    }

}
Run Code Online (Sandbox Code Playgroud)

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

lim*_*imc 26

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


gui*_*ido 13

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

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

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