2013-02-19 50 views
0

我正在使用intellij思想開發javaEE應用程序。讀取xml時出錯 - 一對多關係的映射錯誤

我創建了一個包含3個表(客戶,產品,類別)的mysql數據庫。 然後我使用嚮導生成了休眠配置。

我有以下問題:

févr. 19, 2013 9:40:53 PM org.hibernate.cfg.Configuration addResource 
INFO: HHH000221: Reading mappings from resource: smdatabase/SMCategoryEntity.hbm.xml 
Exception in thread "main" org.hibernate.InvalidMappingException: Unable to read XML 
    at org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:106) 
    at org.hibernate.cfg.Configuration.add(Configuration.java:477) 
    at org.hibernate.cfg.Configuration.add(Configuration.java:473) 
    at org.hibernate.cfg.Configuration.add(Configuration.java:646) 
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:729) 
    at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2105) 
    at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2077) 
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2057) 
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2010) 
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1925) 
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1904) 
    at Main.getSession(Main.java:39) 
    at Main.main(Main.java:49) 
    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:601) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120) 
Caused by: org.dom4j.DocumentException: null Nested exception: null 
    at org.dom4j.io.SAXReader.read(SAXReader.java:484) 
    at org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:76) 
    ... 17 more 

Process finished with exit code 1 

這裏是我的表定義:

CREATE TABLE customer (
    id INT NOT NULL PRIMARY KEY, 
    email VARCHAR(500), 
    password VARCHAR(500), 
    shipping_address VARCHAR(500), 
    shipping_postal_code VARCHAR(500), 
    shipping_country VARCHAR(500), 
    when_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP 
) ENGINE=INNODB; 

CREATE TABLE category(
    id INT NOT NULL auto_increment PRIMARY KEY, 
    name VARCHAR(500) NOT NULL, 
    when_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP 
) ENGINE=INNODB; 

CREATE TABLE product (
    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, 
    price DECIMAL, 
    name VARCHAR(500) NOT NULL, 
    category_id INT NOT NULL, 
    product_index INT NOT NULL default 0, 
    when_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP, 
    FOREIGN KEY (category_id) REFERENCES category(id) 
) ENGINE=INNODB; 

而產生的hbm.xmls(3個不同的文件):

<?xml version='1.0' encoding='utf-8'?> 
<hibernate-mapping xmlns="http://www.hibernate.org/xsd/hibernate-mapping" 
     xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-mapping http://www.hibernate.org/xsd/hibernate-mapping/hibernate-mapping-4.0.xsd" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 

    <class name="smdatabase.SMProductEntity" table="product" catalog="supmarket"> 
     <id name="id"> 
      <column name="id" sql-type="int" length="10" not-null="true"/> 
     </id> 
     <property name="price"> 
      <column name="price" sql-type="decimal" length="10"/> 
     </property> 
     <property name="name"> 
      <column name="name" sql-type="varchar" length="500" not-null="true"/> 
     </property> 
     <property name="categoryId"> 
      <column name="category_id" sql-type="int" length="10" not-null="true"/> 
     </property> 
     <property name="whenCreated"> 
      <column name="when_created" sql-type="timestamp" length="19" not-null="true"/> 
     </property> 
     <many-to-one name="Category" class="smdatabase.SMCategoryEntity"/> 
    </class> 
</hibernate-mapping> 

<!-- file 2 --> 
<?xml version='1.0' encoding='utf-8'?> 

<hibernate-mapping xmlns="http://www.hibernate.org/xsd/hibernate-mapping" 
     xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-mapping http://www.hibernate.org/xsd/hibernate-mapping/hibernate-mapping-4.0.xsd" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 

    <class name="smdatabase.SMCategoryEntity" table="category" > 
     <id name="id"> 
      <column name="id" sql-type="int" length="10" not-null="true"/> 
     </id> 
     <property name="name"> 
      <column name="name" sql-type="varchar" length="500" not-null="true"/> 
     </property> 
     <property name="whenCreated"> 
      <column name="when_created" sql-type="timestamp" length="19" not-null="true"/> 
     </property> 
     <list name="CategoryProducts" inverse="true" table="product"> 
      <key column="category_id" /> 
      <list-index column="product_index" base="1"/> 
      <one-to-many not-found="ignore" class="smdatabase.SMProductEntity"/> 
     </list> 
    </class> 
</hibernate-mapping> 

<!-- file 3 --> 
<?xml version='1.0' encoding='utf-8'?> 
<hibernate-mapping xmlns="http://www.hibernate.org/xsd/hibernate-mapping" 
     xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-mapping http://www.hibernate.org/xsd/hibernate-mapping/hibernate-mapping-4.0.xsd" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 

    <class name="smdatabase.SMCustomerEntity" table="customer" schema="" catalog="supmarket"> 
     <id name="id"> 
      <column name="id" sql-type="int" length="10" not-null="true"/> 
     </id> 
     <property name="email"> 
      <column name="email" sql-type="varchar" length="500"/> 
     </property> 
     <property name="password"> 
      <column name="password" sql-type="varchar" length="500"/> 
     </property> 
     <property name="shippingAddress"> 
      <column name="shipping_address" sql-type="varchar" length="500"/> 
     </property> 
     <property name="shippingPostalCode"> 
      <column name="shipping_postal_code" sql-type="varchar" length="500"/> 
     </property> 
     <property name="shippingCountry"> 
      <column name="shipping_country" sql-type="varchar" length="500"/> 
     </property> 
     <property name="whenCreated"> 
      <column name="when_created" sql-type="timestamp" length="19" not-null="true"/> 
     </property> 
    </class> 
</hibernate-mapping> 

我已更改類別表中的屬性。它生成後是:

<list name="CategoryProducts" inverse="true"> 
    <key /> 
    <one-to-many not-found="ignore" class="smdatabase.SMProductEntity"/> 
</list> 

我完全失去了。有人可以幫我嗎?

回答

1

我不知道你提到的XSD是有效的:

http://www.hibernate.org/xsd/hibernate-mapping/hibernate-mapping-4.0.xsd 

你應該堅持以下,即使你使用Hibernate 4:

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

謝謝您的回答。這是一個很好的抓住! – Bastian 2013-02-22 18:28:08

+0

不客氣。我很驚訝地發現,IntelliJ似乎在版本4.0中知道這個XSD,但是Hibernate不會...... – 2013-02-25 09:58:33