-->
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.  [ 2 posts ] 
Author Message
 Post subject: Collection deleted and reinserted on load(Clazz.class, id)
PostPosted: Thu May 25, 2006 4:53 am 
Senior
Senior

Joined: Fri Jun 18, 2004 10:17 am
Posts: 140
Hello,

I am finding a behaviour I do not understand and would appreciate some insight if possible.

A Page has a collection of Asset. But in order to get additional attributes of the relationship it is modelled with a composite element. Therefore a Page has a collection of PageAsset, where PageAsset has an Asset and the additional properties.

I am finding that simply trying to load a Page by ID with

Code:
load(Page.class, id)


is causing deletion of all PageAssets associated with the Page, and then reinsertion immediately.

I then get the Page as expected. However, I would like to understand why Hibernate deletes the whole collection before immediately recreating it as it's causing performance issues, I see little reason why it should do this since the data is the same before it deletes and after it reinserts, so perhaps I have modelled something incorrectly.

Your help appreciated!

Hibernate version: 3.0

Mapping documents:

Code:
    <class name="Page" table="tblPages" lazy="false">

        <id name="id" type="int" column="page_id" unsaved-value="0">           
           <generator class="identity" />
       </id> 

        <set name="assets" table="tblPageAssets" lazy="false" cascade="none">
            <key column="page_id" not-null="true" />
           
            <composite-element class="PageAsset">
            <many-to-one name="asset" class="Asset" column="asset_id" cascade="all" />
            <property name="peerPosition" column="peer_position" type="int" not-null="true" />
            <property name="zone" column="zone" type="string" not-null="true" />
         </composite-element>
        </set>

</class>


Code between sessionFactory.openSession() and session.close():

getHibernateTemplate().load(Page.class, id);

Name and version of the database you are using:

MSSQL2K

The generated SQL (show_sql=true):

Hibernate: delete from dbWeb.dbo.tblPageAssets where page_id=? and peer_position=? and zone=?
Hibernate: insert into dbWeb.dbo.tblPageAssets (page_id, asset_id, peer_position, zone) values (?, ?, ?, ?)
Hibernate: insert into dbWeb.dbo.tblPageAssets (page_id, asset_id, peer_position, zone) values (?, ?, ?, ?)
Hibernate: insert into dbWeb.dbo.tblPageAssets (page_id, asset_id, peer_position, zone) values (?, ?, ?, ?)
Hibernate: insert into dbWeb.dbo.tblPageAssets (page_id, asset_id, peer_position, zone) values (?, ?, ?, ?)


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 25, 2006 5:28 am 
Senior
Senior

Joined: Fri Jun 18, 2004 10:17 am
Posts: 140
my bad ... did not implement equals and hashCode :)

Code:
   @Override
   public boolean equals(Object o) {
      if (this == o) return true;
      if (! (o instanceof PageAsset)) return false;
      final PageAsset that = (PageAsset) o;
      
      return
         (this.getAsset().getId() == that.getAsset().getId()) &&
         (this.getPeerPosition() == that.getPeerPosition()) &&
         (this.getZone().equals(that.getZone()));
   }
   
   @Override
   public int hashCode() {
      return (this.getAsset().getId() + this.getPeerPosition() +
         this.getZone()).hashCode();
   }


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.