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 (?, ?, ?, ?)