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