2012-08-25 37 views
2

我在一個maven項目中工作。 Atm我只有實體,映射和配置文件。我試圖用junit測試我的hibernate映射。Hibernate SaxParseException

我找不到問題。我可以驗證netbeans中的xml文件,但是當我運行我的測試時,出現以下錯誤。

驗證在NetBeans:

XML validation started. 

Checking file:/C:/Users/Err/Documents/NetBeansProjects/PinkSocks/src/main/resources/hibernate.cfg.xml... 
Referenced entity at "http://localhost:8080/files/hibernate-configuration-4.0.xsd". 
XML validation finished. 

錯誤:

Initial SessionFactory creation failed.org.hibernate.MappingException: invalid configuration 
      at bleh.mihihi.utils.HibernateUtil.buildSessionFactory(HibernateUtil.java:25) 
      at bleh.mihihi.utils.HibernateUtil.<clinit>(HibernateUtil.java:16) 
      at bleh.mihihi.utils.HibernateUtilTest.setUp(HibernateUtilTest.java:20) 
    Caused by: org.hibernate.MappingException: invalid configuration 
      at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2014) 
      at org.hibernate.cfg.Configuration.configure(Configuration.java:1931) 
      at org.hibernate.cfg.Configuration.configure(Configuration.java:1910) 
      at bleh.mihihi.utils.HibernateUtil.buildSessionFactory(HibernateUtil.java:21) 
    Caused by: org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 25; Document is invalid: no grammar found. 
      at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:198) 

hibernate.cfg.xml中:

<?xml version="1.0" encoding="UTF-8"?> 
    <hibernate-configuration 
     xmlns="http://www.hibernate.org/xsd/hibernate-configuration" 
     xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' 
     xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-configuration http://localhost:8080/files/hibernate-configuration-4.0.xsd"> 
     <session-factory> 
      <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 
      <property name="connection.url">jdbc:mysql://localhost/pinksocks</property> 
      <property name="connection.username">root</property> 
      <property name="connection.password">admin</property> 
      <property name="connection.pool_size">1</property> 
      <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 
      <property name="current_session_context_class">thread</property> 
      <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> 
      <property name="show_sql">true</property> 
      <mapping class="bleh.mihihi.Beverage" file="Beverage.hbm.xml" /> 
     </session-factory> 
    </hibernate-configuration> 

Beverage.hbm.xml

<?xml version="1.0" encoding="UTF-8"?> 
    <hibernate-mapping 
    xmlns="http://www.hibernate.org/xsd/hibernate-mapping" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-mapping http://localhost:8080/files/hibernate-mapping-4.0.xsd" 
     package="bleh.mihihi.entities"> 
     <class name="Beverage" table="beverages"> 
      <id name="id" column="id"> 
       <generator class="native"/> 
      </id> 
      <property name="name" column="name" /> 
      <property name="alcohol" column="alcohol"/> 
     </class> 
    </hibernate-mapping> 

在此先感謝

ERR

EDIT1:

事情我想:

  • 重新輸入

  • 使用DTD

  • 將XSD到其他地方

+0

嘗試刪除hibernate.cfg.xml中的行並自己寫一遍。看看它是否有效!可能是 instanceOfObject

+0

使用junit進行測試時可以使用此位置嗎? http:// localhost:8080/files/hibernate-configuration-4.0.xsd – dcernahoschi

+0

Retyping did not work。 xsd文件與項目分開部署。 – Err

回答

6

使用Hibernate 4.x中你應該使用相同的DTD 3.X:你可以

<?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> 

請參閱此bug report,其中棄用警告已刪除。 另外4.2版本的教程使用這個,你可以看到here

3

你忘了指定DTD。下面兩個XML文件中的代碼

建議保持,讓我知道

<!DOCTYPE hibernate-mapping PUBLIC 
     "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
+0

我想使用4.0 xsd文件而不是DTD。我曾嘗試切換,但也沒有工作。 – Err

+0

我遇到了同樣的問題 – amphibient

+0

我也遇到過這個問題。 – Kaoet

0

您忘記了hibernate.cfg.xml文件中的DTD