2015-05-14 75 views
-2

所以我有一個hibernate的問題,因爲它以某種方式不再工作了。 不知何故,它工作了很長一段時間,現在它停止工作。所以我要麼是啞巴,要麼是盲目的。或兩者? :P 因此,這裏是我的代碼:雖然hibernate存在,但它不會找到文件

configFile = System.getProperty("user.dir"); 
configFile += File.separator + "src" 
      + File.separator + "main" 
      + File.separator + "resources" 
      + File.separator; 

configFile += "hibernate.cfg.xml"; 
File file = new File(configFile); 
System.out.println(file.exists()); 
System.out.println(file.getAbsolutePath()); 
Configuration configuration = new Configuration(); 
configuration.configure(configFile); 

控制檯甚至有傳言稱該文件存在:

true 
/Users/Bernhard1/Dropbox/Ausgaben2/src/main/resources/hibernate.cfg.xml 

但我得到這個異常:

Exception in thread "main" org.hibernate.HibernateException: /Users/Bernhard1/Dropbox/Ausgaben2/src/main/resources/hibernate.cfg.xml not found 

任何想法?

+0

Hibernate從類路徑中查找文件,然後試圖給出文件的確切位置?這是無效的 – Chaitanya

回答

0
 
Just use: 
configuration.configure("hibernate.cfg.xml"); 
And give it a try. 
Hibernate will pick this file up from resources folder. 
+0

感謝您的回覆,但我仍然得到相同的錯誤 – user2529173

+0

你可以將這個文件放在src目錄下,並嘗試?通常,它從src目錄獲取相對路徑。 – shruti1810

+0

如果您將「資源」目錄標記爲「資源根目錄」,則只需提及其名稱就可以找到該文件。 – shruti1810

0

如果您正在使用Maven的項目,然後只是嘗試加載像這樣的文件:

Configuration configuration = new Configuration(); 
configuration.configure("hibernate.cfg.xml"); 

假設它是Maven項目那麼該文件將被放置在類路徑是根target/classes目錄。

Hibernate從你的類路徑的根目錄查找文件,那麼它應該工作。 Hibernate不會嘗試根據你正在嘗試的絕對路徑來查找文件,這是不正確的。

+0

我試過了。我在線程「main」中得到相同的錯誤異常org.hibernate.HibernateException:找不到hibernate.cfg.xml – user2529173

+0

@ user2529173,用你試過的代碼更新你的問題。你還沒有確認你在使用Maven。我的回答是假設您正在使用maven並在某些IDE(如Eclipse)上運行該程序 – Chaitanya

相關問題