Bastian
Bastian

Reputation: 23

Error reading xml - wrong mapping for one to many relation

I am using intellij idea to develop javaEE applications.

I have created a mysql database with 3 tables (customer, product, category). Then I generated the hibernate configuration using the wizard.

I have the following issue:

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

Here is my tables definition:

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;

And the hbm.xmls generated (3 different files):

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

I have changed the property in the category table. After it has been generated it was:

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

I am totally lost. Does anybody could help me please?

Upvotes: 0

Views: 1216

Answers (1)

Bastien Jansen
Bastien Jansen

Reputation: 8846

I'm not sure that the XSD you mention is valid:

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

You should stick with the following, even if you are using 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>

Upvotes: 1

Related Questions