读取xml时出错 - 一对多关系的错误映射

时间:2013-02-19 20:58:42

标签: hibernate java-ee intellij-idea

我正在使用intellij的想法来开发javaEE应用程序。

我创建了一个包含3个表(客户,产品,类别)的mysql数据库。 然后我使用向导生成了hibernate配置。

我有以下问题:

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 个答案:

答案 0 :(得分: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>