-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Failure generating schema files with HibernateToolTask
PostPosted: Sun Apr 23, 2006 5:06 pm 
Beginner
Beginner

Joined: Sun Feb 19, 2006 5:13 am
Posts: 22
Hi

Im uding the HIbernateToolTask to generate my schema files. The problem I encounter is when Im generating the schema for a many-to-many mapping. Below are the XML files. I have a category and a record mapping which has a unidirectional many-to-many mapping from category to record. The map table is CategoryToRecord, which I also map with a CategoryToRecord.hbm.xml. Since I want the Category maping to own the relationship i have set inverse to false in the set mapping of records in category. That is I want the category to own the relationship

The CategoryToRecord table should look something like this to be Ok:

Code:
CREATE TABLE `CategoryToRecord` (
    `id` int(11) NOT NULL auto_increment PRIMARY KEY,
    `categoryId` int(11),
    `recordId` int(11),
    CONSTRAINT FOREIGN KEY(categoryId) REFERENCES Category(`id`),
    CONSTRAINT FOREIGN KEY(recordId) REFERENCES Record(`id`)
);



But in reality it look like this, that is the categoryId becomes the primary key that is auto incremented, which is wrong:

Code:
CREATE TABLE `CategoryToRecord` (
    `id` int(11) NOT NULL,
    `categoryId` int(11) auto_increment PRIMARY KEY,
    `recordId` int(11) PRIMARY KEY,
    CONSTRAINT FOREIGN KEY(categoryId) REFERENCES Category(`id`),
    CONSTRAINT FOREIGN KEY(recordId) REFERENCES Record(`id`)
);


The problem seems to go way when i set the inverse mapping of category to records to true but thats not the behaivour I am looking for since I want the CategoryToRecord mapping to be affcted when I add records to the category.


CATEGORY MAPPING
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="se.so4it.dam.api.category.Category" proxy="se.so4it.dam.api.category.Category" table="Category" lazy="true">
        <comment>todo</comment>
        <id name="id" column="id" type="long" unsaved-value="null">
            <generator class="native"/>
        </id>
        <property name="guid" type="string" unique="true" lazy="true">
            <column name="guid" unique="true" not-null="true"><comment>Globaly unique identifier of Categories</comment></column>
        </property>
        <property name="name" type="string" lazy="true">
            <column name="name"><comment>todo</comment></column>
        </property>
        <property name="level" type="integer" lazy="true">
            <column name="level"><comment>todo</comment></column>
        </property>
        <set name="categories" access="field" inverse="true" cascade="all" lazy="true" batch-size="10">
            <comment>todo</comment>
            <key column="parentId"/>
            <one-to-many class="se.so4it.dam.api.category.Category"/>
        </set>
        <many-to-one name="parent" column="parentId" not-null="false" class="se.so4it.dam.api.category.Category" lazy="proxy"/>
        <set name="records" access="field" table="CategoryToRecord" inverse="true" cascade="none" lazy="true" batch-size="10">
            <comment>todo</comment>
            <key column="categoryId"/>
            <many-to-many class="se.so4it.dam.api.record.Record" column="recordId"/>
        </set>
        <set name="recordCollections" access="field" table="CategoryToRecordCollection" inverse="true" cascade="none" lazy="true" batch-size="10">
            <comment>todo</comment>
            <key column="categoryId"/>
            <many-to-many class="se.so4it.dam.api.recordcollection.RecordCollection" column="recordCollectionId"/>
        </set>
    </class>
    <query name="category.exists"><![CDATA[select count(*) from Category c where c.id = :var]]></query>
</hibernate-mapping>



CATEGORY TO RECORD MAPPING

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="se.so4it.dam.server.category.CategoryToRecord" table="CategoryToRecord" lazy="false">
        <comment>todo</comment>
        <id name="id" access="field" column="id" type="long">
            <generator class="native"/>
        </id>
        <many-to-one
            name="category"
            class="se.so4it.dam.api.category.Category"
            outer-join="auto"
            column="categoryId"/>
        <many-to-one
            name="record"
            class="se.so4it.dam.api.record.Record"
            outer-join="auto"
            column="recordId"/>
    </class>
    <query name="remove.record.from.category">delete from CategoryToRecord cr where cr.record = :var</query>
    <query name="remove.records.from.category"><![CDATA[delete from CategoryToRecord ctr where ctr.record in (:var1) and ctr.category = :var2]]></query>
</hibernate-mapping>


I would appriciate any hint on what Im doing wrong here. I use Hibernate 3.1.3 core.

Cheers
Magnus


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.