2011-02-08 13 views
10

私は春3 mvcで休止状態を使用しようとしていますが、現時点ではこの例外がスローされます。私はどこかで私の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(); 

が、私はそれが正しくない推測しています?以下

src/com/jr/hibernate/は私のcfgファイルで内部の私は現在、私の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); 
     } 
     } 

} 

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

歓声。できます。私は別の質問があります。私のアプリケーションは、私のハイバーネームxmlマッピングを見つけることができない。私はcom.jr.hibernateMappingsに配置していますが、build.xml doesntはhbm.xmlファイルをビルドしてコンパイルするようです。もう一度間違った場所にいますか? WAR/WEB-INF/classesフォルダにある必要がありますか?可能であればsrcディレクトリ内にあることを好むでしょう – jonney

+0

ありがとう...私のファイル(hibernate.cfg.xml)をsrcに移動した後、とても感謝します。 –

13

hibernate.cfg.xmlは、webappの起動時にクラスパスのルートになければなりません。

mavenを使用してプロジェクトをビルドする場合は、ディレクトリにhibernate.cfg.xmlを入れて、warパッケージをビルドするときに自動的に/WEB-INF/classesに配置します。

mavenを使用しない場合は、ファイルを直接WEB-INF/classesディレクトリに置きます。

4

hibernate.cfg.xmlは、WEB-INF/classesである必要があります。または、対応する引数をconfigure(..)メソッドに渡すことで、カスタムの場所からロードすることもできます。

+0

Bozho、 'config.configure("/resources/hibernate.cfg.xml ");'は私にとっては機能しません。実際、私は単純な「休止状態の3.1」アプリをやっています。 Btw、 'config'は' AnnotationConfiguration'型です。 –

0

IntelliJでは、「プロジェクト設定を開く」>>モジュール>>プロジェクトで使用されている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に置く必要があります。 。その後、あなたのアプリケーションを実行するクラスでちょうど行挿入します。=新しい 設定()を設定( "hibernate.cfg.xmlの")

のSessionFactoryの工場をaddAnnotatedClassを()。buildSessionFactory();

+0

Thnx。私はこの2日後にほとんど無駄にした。 – SAM

関連する問題