-->
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.  [ 10 posts ] 
Author Message
 Post subject: Delete and Insert records from joining table while selecting
PostPosted: Thu Mar 19, 2009 9:11 am 
Newbie

Joined: Thu Mar 19, 2009 7:59 am
Posts: 5
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.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 19, 2009 9:42 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Could it be because your Category.getCategoryItems() method is not returning the same Set instance as was passed by Hibernate to Category.setCategoryItems()?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 20, 2009 12:11 am 
Newbie

Joined: Thu Mar 19, 2009 7:59 am
Posts: 5
Hi nordborg,
Thanks for the reply.But the getter and setter or Caegory for categoryItems is returning the same set of instances.
More over its not throwing any exceptions and loading the data properly.
But the only problem I see here is it is firing the delete and insert queries on the mapping table(the last 2 queries in the show sql log that i pasted).


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 20, 2009 3:14 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
same set of instances.


Hmm... to me this is not the same as "the same Set instance". Here is an example of what it should look like. You must never set 'this.items' to some other object than what Hibernate is passing on in the setCategoryItems method.

Code:
private Set items;
public void setCategoryItems(Set items)
{
   this.items = items;
}
public Set getCategoryItems()
{
   return items;
}

[/quote]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 20, 2009 3:34 am 
Newbie

Joined: Thu Mar 19, 2009 7:59 am
Posts: 5
The declaration of the CategoryItens set and its setter/getters are as follows in the Category class:

Code:
private Set<CategoryItem> categoryItems = new HashSet<CategoryItem>(0);
     
     public Set<CategoryItem> getCategoryItems() {
         return this.categoryItems;
     }     
     public void setCategoryItems(Set<CategoryItem> categoryItems) {
         this.categoryItems = categoryItems;
     }


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 20, 2009 3:38 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
That seems ok. Then you should look for something else that manipulates your Set.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 20, 2009 6:16 am 
Newbie

Joined: Thu Mar 19, 2009 7:59 am
Posts: 5
I have checked,but no where it is happening so. Infact i am able to travese the object graph completely without any problem.
But the only issue is it is firing the additional delete and insert queries which are htting the performance and forcing a synchronized data base call for fetching data.


Last edited by ParameshAnde on Fri Mar 20, 2009 6:33 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 20, 2009 6:32 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
The only reason I can think of for this to happen is that Hibernate thinks that the set has been manipulated. Can you boil it down to a simple test case?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 20, 2009 9:43 am 
Newbie

Joined: Thu Mar 19, 2009 7:59 am
Posts: 5
Here is the tester logic :

Code:
   @Test
      public void testRetreiveCategoryItem()throws Exception{            
             Category cat=catItemDao.getCategoryById("1");
             Set<CategoryItem> catItemList=cat.getCategoryItems();
             CategoryItem catItem= catItemList.iterator().next();
             Item item=catItem.getItem();
         
      }



And the DAO logic:


Code:
public Category getCategoryById(String catId) throws Exception
   {
      Category cat=null;
      try
      {   
         cat=(Category) getHibernateTemplate().load(Category.class, catId);
         
      } catch (Exception e) {   
         log.error("Error inside method JobInfoDaoImpl.getSMSInboundJobDetails(). Probable Reason :: ", e);
         
      
      }
      return cat;
   }


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 20, 2009 10:26 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I have no idea why this should cause the behaviour you describe.


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

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.