2011-02-08 65 views
10

我想用spring 3 mvc的hibernate,但是现在我得到这个异常抛出。我想我需要在某处定义我的hibernate.cfg.xml,但不知道在哪里?org.hibernate.HibernateException:/hibernate.cfg.xml找不到

我基本上遵循这个例子在这里http://www.nabeelalimemon.com/blog/2010/05/spring-3-integrated-with-hibernate-part-a/在特别看到这行代码那里,想为“神奇”利用找到我hibernate.cfg文件这样的:

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

我猜测是不正确的?目前,我有我的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> 

我休眠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<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); 

} 

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

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

} 

提前干杯。我也在我的hibernate.cfg.xml文件所在的位置拥有我所有的表hibvernate xml映射

回答

25

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

+0

欢呼声。有用。我有另一个问题。我的应用程序似乎无法找到我的hibernate xml映射。我把它放在com.jr.hibernateMappings中,但是build.xml似乎并没有构建和编译我的hbm.xml文件。它又是在错误的位置吗?它是否需要在WAR/WEB-INF/classes文件夹中?如果可能的话,我更愿意将它放在src目录中 – jonney

+0

谢谢...在将我的文件(hibernate.cfg.xml)移动到src后,它会再次找到文件,非常感谢 –

13

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

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

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

4

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

+0

bozho,'config.configure(“/ resources/hibernate.cfg.xml”);'不适合我。其实,我正在做一个简单的'hibernate 3.1'应用程序。顺便说一句,'config'的类型是'AnnotationConfiguration'。 –

0

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

0

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

2

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

SessionFactory的工厂=新 配置()配置( “hibernate.cfg.xml中”)addAnnotatedClass()。buildSessionFactory();

+0

Thnx。在此之后的两天我几乎浪费了。 – SAM