Hi, I am using
Hibernate version:3.2
I have a many to many relationship between two tables Category and Item for which i used a joining table Category_Item.The joining table is having an additional column createdUser apart from the primary keys of the parent tables. The problem i am getting here is while fetching the data from Category table insert and delete queries are being fired on the mapping table which is not required.
Mapping file for Category table:
Code:
<hibernate-mapping>
<class name="com.hibernate.item.Category" table="CATEGORY" schema="CXCSUSR">
<id name="id" type="java.lang.String">
<column name="ID" length="47" />
<generator class="uuid.hex" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" length="100" />
</property>
<property name="description" type="java.lang.String">
<column name="DESCRIPTION" length="100" />
</property>
<property name="createduser" type="java.lang.String">
<column name="CREATEDUSER" length="100" />
</property>
<property name="createdDate" type="java.util.Date">
<column name="CREATED_DATE" length="7" />
</property>
<set name="categoryItems" cascade="all" table="CATEGORY_ITEM" lazy="false" >
<key> <column name="CAT_ID" length="47" not-null="true"></column></key>
<composite-element class="com.hibernate.item.CategoryItem">
<parent name="category" />
<many-to-one name="item" class="com.hibernate.item.Item" column="ITEM_ID" cascade="all" lazy="false"></many-to-one>
<property name="createduser" column="CREATEDUSER"></property>
</composite-element>
</set>
</class>
</hibernate-mapping>
Mapping file for Item table:
Code:
<hibernate-mapping>
<class name="com.hibernate.item.Item" table="ITEM" schema="CXCSUSR">
<id name="id" type="java.lang.String">
<column name="ID" length="47" />
<generator class="uuid.hex" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" length="47" />
</property>
<property name="description" type="java.lang.String">
<column name="DESCRIPTION" length="100" />
</property>
<property name="createduser" type="java.lang.String">
<column name="CREATEDUSER" length="100" />
</property>
</class>
</hibernate-mapping>
Quote:
The hibernate generated sql is:
Code:
Hibernate: select category0_.ID as ID10_, category0_.NAME as NAME10_, category0_.DESCRIPTION as DESCRIPT3_10_, category0_.CREATEDUSER as CREATEDU4_10_, category0_.CREATED_DATE as CREATED5_10_ from CXCSUSR.CATEGORY category0_ where category0_.ID=?
Hibernate: select categoryit0_.CAT_ID as CAT1_0_, categoryit0_.ITEM_ID as ITEM2_0_, categoryit0_.CREATEDUSER as CREATEDU3_0_ from CATEGORY_ITEM categoryit0_ where categoryit0_.CAT_ID=?
Hibernate: select item0_.ID as ID12_0_, item0_.NAME as NAME12_0_, item0_.DESCRIPTION as DESCRIPT3_12_0_, item0_.CREATEDUSER as CREATEDU4_12_0_ from CXCSUSR.ITEM item0_ where item0_.ID=?
Hibernate: delete from CATEGORY_ITEM where CAT_ID=? and ITEM_ID=? and CREATEDUSER=?
Hibernate: insert into CATEGORY_ITEM (CAT_ID, ITEM_ID, CREATEDUSER) values (?, ?, ?)
Could some body suggest how to avoid these unwanted insert delete queries being fired.