2016-02-27 125 views
1

我在IntelliJ IDE中遇到了我的hibernate.cfg.xml問題。Hibernate ConfigurationException:找不到cfg.xml資源[hibernate.cfg.xml] IntelliJ

這裏是我的Hibernate的配置文件:

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

<hibernate-configuration> 
    <session-factory> 
     <!-- Database connection settings --> 
     <property name="connection.driver:class">org.postgresql.Driver</property> 
     <property name="connection.url">jdbc:postgresql://localhost/HIndex</property> 
     <property name="hibernate.connection.username">index_user</property> 
     <property name="hibernate.connection.password">password</property> 

     <!-- JDBC connection pool (use the built-in) --> 
     <property name="connection.pool_size">1</property> 

     <!-- SQL Dialect --> 
     <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQL82Dialect</property> 

     <!-- Assume test is the database name --> 
     <property name="show_sql">true</property> 

     <!-- Drop and re-create the database schema on startup --> 
     <property name="hbm2ddl.auto">create</property> 

     <!-- Names the annotated entity class --> 
     <mapping class="HIndexSaar.HIndex.Person"/> 
     <mapping class="HIndexSaar.HIndex.University"/> 
     <mapping class="HIndexSaar.HIndex.Publication"/> 
    </session-factory> 
</hibernate-configuration> 

這是我的應用程序類:

package HIndexSaar.HIndex; 

public class AppHibernate { 

    public static void main(String[] args){ 
     HibernateManager mng = new HibernateManager(); 
     [...] 
    } 
} 

所以:

package HIndexSaar.HIndex; 

import org.hibernate.HibernateException; 
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.Transaction; 
import org.hibernate.cfg.Configuration; 

public class HibernateManager { 

    private static SessionFactory factory; 

    public HibernateManager(){ 
     // 
     //* Setup the configuration. 
     // 
     Configuration config = new Configuration().configure("hibernate.cfg.xml").addAnnotatedClass(Person.class)    .addAnnotatedClass(University.class).addAnnotatedClass(Publication.class); 
     factory = config.buildSessionFactory(); 
    } 

我通過運行下面的代碼得到一個錯誤在HibernateManager中構建SessionFactory似乎有些問題。我得到的錯誤

無法找到cfg.xml中資源

和以下堆棧跟蹤:

Feb 27, 2016 10:26:03 PM org.hibernate.Version logVersion 
INFO: HHH000412: Hibernate Core {5.0.7.Final} 
Feb 27, 2016 10:26:03 PM org.hibernate.cfg.Environment <clinit> 
INFO: HHH000206: hibernate.properties not found 
Feb 27, 2016 10:26:03 PM org.hibernate.cfg.Environment buildBytecodeProvider 
INFO: HHH000021: Bytecode provider name : javassist 
Exception in thread "main"  org.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource [hibernate.cfg.xml] 
at org.hibernate.boot.cfgxml.internal.ConfigLoader.loadConfigXmlResource(ConfigLoader.java:53) 
at org.hibernate.boot.registry.StandardServiceRegistryBuilder.configure(StandardServiceRegistryBuilder.java:163) 
at org.hibernate.cfg.Configuration.configure(Configuration.java:259) 
at org.hibernate.cfg.Configuration.configure(Configuration.java:245) 
at HIndexSaar.HIndex.HibernateManager.<init>(HibernateManager.java:18) 
at HIndexSaar.HIndex.AppHibernate.main(AppHibernate.java:6) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:606) 
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) 

我已經劃歸hibernate.cfg.xml文件/ src目錄/主要/ java /資源,但顯然有些事情仍然是錯誤的。 有人知道我犯了什麼錯誤嗎?令人驚訝的是,相同的代碼在Eclipse中運行沒有錯誤,所以可能是IntelliJ的問題?我使用相同的SDK/JDK,即jdk 1.7。

+0

將在src文件夾下的hibernate.cfg.xml文件,然後嘗試執行程序 –

+0

只是去嘗試,但如果您使用的是IntelliJ沒有解決的問題:( – tschens

+0

然後嘗試創建具有hibernate框架支持的新模塊,然後粘貼相同的代碼並執行它 –

回答

0

如果你正在使用intellij然後嘗試創建新的模塊與hibernate框架支持,然後粘貼相同的代碼並執行它。 這可能是在clipse和IntelliJ程差