2015-12-16 116 views
0

我正在嘗試使用Hibernate withing我的應用程序時出現以下錯誤:Hibernate:無法解析配置:hibernate.cfg.xml?

org.hibernate.HibernateException: Could not parse configuration: hibernate.cfg.xml 

是什麼原因造成的?請參閱下面的hibernate.cfg.xml,outputRunner class

注意:我已經看過this answer,並嘗試使用建議修復它,但沒有運氣。

hibernate.cfg.xml中:

<?xml version='1.0' encoding='utf-8'?> 
    <!DOCTYPE hibernate-configuration PUBLIC 
      "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
      "http://www.hibernate.org/dtd/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/test-db</property> 
      <property name="connection.username">user</property> 
      <property name="connection.password">password</property> 

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

      <!-- Echo all executed SQL to stdout --> 
      <property name="show_sql">true</property> 

      <!-- Use XML-based mapping metadata --> 
      <!-- <mapping resource="domain/Message.hbm.xml"/> --> 

      <!-- Use Annotation-based mapping metadata --> 
      <mapping class="entity.Message"/>    

     </session-factory> 
    </hibernate-configuration> 

輸出:

Successfully started process 'command 'C:\Program Files\Java\jdk1.8.0_65\bin\java.exe'' 
Dec 16, 2015 12:01:20 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit> 
INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final} 
Dec 16, 2015 12:01:20 AM org.hibernate.Version logVersion 
INFO: HHH000412: Hibernate Core {4.3.5.Final} 
Dec 16, 2015 12:01:21 AM org.hibernate.cfg.Environment <clinit> 
INFO: HHH000206: hibernate.properties not found 
Dec 16, 2015 12:01:21 AM org.hibernate.cfg.Environment buildBytecodeProvider 
INFO: HHH000021: Bytecode provider name : javassist 
Dec 16, 2015 12:01:21 AM org.hibernate.cfg.Configuration configure 
INFO: HHH000043: Configuring from resource: hibernate.cfg.xml 
Dec 16, 2015 12:01:21 AM org.hibernate.cfg.Configuration getConfigurationInputStream 
INFO: HHH000040: Configuration resource: hibernate.cfg.xml 
Initial SessionFactory creation failed.org.hibernate.HibernateException: Could not parse configuration: hibernate.cfg.xml 
Exception in thread "main" java.lang.ExceptionInInitializerError 
    at util.HibernateUtil.buildSessionFactory(HibernateUtil.java:25) 
    at util.HibernateUtil.<clinit>(HibernateUtil.java:12) 
    at client.HelloWorldClient.main(HelloWorldClient.java:13) 
Caused by: org.hibernate.HibernateException: Could not parse configuration: hibernate.cfg.xml 
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2163) 
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2075) 
    at util.HibernateUtil.buildSessionFactory(HibernateUtil.java:18) 
    ... 2 more 
Caused by: org.dom4j.DocumentException: Error on line 2 of document : The processing instruction target matching "[xX][mM][lL]" is not allowed. Nested exception: The processing instruction target matching "[xX][mM][lL]" is not allowed. 
    at org.dom4j.io.SAXReader.read(SAXReader.java:482) 
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2155) 
    ... 4 more 
:run FAILED 
:run (Thread[Daemon worker Thread 15,5,main]) completed. Took 0.617 secs. 

FAILURE: Build failed with an exception. 

* What went wrong: 
Execution failed for task ':run'. 
> Process 'command 'C:\Program Files\Java\jdk1.8.0_65\bin\java.exe'' finished with non-zero exit value 1 

* Try: 
Run with --stacktrace option to get the stack trace. Run with --debug option to get more log output. 

BUILD FAILED 

亞軍類:

package client; 

import org.hibernate.Session; 

import util.HibernateUtil; 
import entity.Message; 


public class HelloWorldClient { 
    public static void main(String[] args) { 

       Session session = HibernateUtil.getSessionFactory().openSession(); 
       session.beginTransaction(); 

       Message message = new Message("Hello World with Hibernate & JPA Annotations"); 

       session.save(message); 

       session.getTransaction().commit(); 
       session.close(); 

    } 
} 

編輯:將鼠標懸停在會話的出廠標籤時看到錯誤:

enter image description here

回答

1

你可以檢查下面我用過的CFG文件,並確保CFG文件將出現在SRC目錄下。如果您保留在另一個目錄中,請確保在獲取配置實例的同時傳遞路徑。

<?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> 
     <property name="hibernate.connection.driver_class"> com.mysql.jdbc.Driver </property> 
     <property name="hibernate.connection.url"> jdbc:mysql://localhost:3306/hibernatex </property> 
     <property name="hibernate.connection.username"> root </property> 
     <property name="hibernate.connection.password"> root </property> 
     <property name="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </property> 
     <mapping resource="com/ora/hibernate/examples/Employee.hbm.xml" 
      package="com.ora.hibernate.examples" /> 
    </session-factory> 
</hibernate-configuration> 
+0

謝謝,我找到了答案,那是我的資源目錄下沒有CFG文件 – java123999

0

爲了被解析爲XML,把hibernate.cfg.xml應該這樣開始:

<?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.cfg.xml:

  1. 檢查是否有以前<?xml ?>
  2. 012一些空格或其他內容
  3. 檢查是否有在hibernate.cfg.xml另一個<?xml ?>定義
  4. 檢查,如果存在<?xml ?>http://www.w3.org/International/questions/qa-byte-order-mark#remove)之前字節順序標記(BOM)
+0

對不起,我貼錯了,我也有類似這樣的東西。請參閱我的編輯 – java123999

+0

嘗試我張貼的片段。 – user987339

+0

我做了,我得到完全相同的錯誤信息? – java123999

0

蹊蹺的配置的第二行文件。嘗試更改DTD文件的位置,如下所示。

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

More Info

希望這有助於。

+0

嗨,我試過這個,但沒有運氣。不幸的是出現了相同的錯誤 – java123999

+0

刪除'<?xml'標記之前的所有空格和新行,並在構建之前清理您的項目。 –

+0

謝謝,如何在Intelij中清理項目? – java123999

0

我能解決問題所在。問題是我的hibernate.cfg.xml文件不在我的項目內的「資源」目錄下。

這解決了我所遇到的所有問題,並且我做了而不是需要更改文件內的代碼。

1

這是因爲您的互聯網阻止從該DTD獲取數據。 只是dowbload所有DTD是提xml文件中,並在所有的XML文件給位置這樣的.. 這對我的作品

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "C:\Downloads\hibernate-mapping-3.0.dtd"> <hibernate-configuration> 
相關問題