2010-07-27 36 views
3

這裏是我的映射的一部分:休眠:在刪除+插入組合元素結果在每次提交

<hibernate-mapping package="trx.domain"> 
    <class name="Master" table="master" dynamic-update="true" dynamic-insert="true"> 

     <set name="attributes" table="attribute" lazy="true" 
      cascade="all" batch-size="10"> 
      <cache usage="nonstrict-read-write" /> 
      <key> 
       <column name="master_id" /> 
      </key> 
      <composite-element class="Attribute"> 
       <many-to-one name="type" class="AttributeType" 
        not-null="true" column="attribute_type_id" lazy="false" /> 
       <property name="value"> 
        <column name="value" /> 
       </property> 
      </composite-element> 
     </set> 
    </class> 
</hibernate-mapping> 

如果我只是掃描attributes集,沒有任何更新,休眠仍將執行批處理的delete並提交insert操作。

Hibernate: delete from attribute where master_id=? and attribute_type_id=? 
Hibernate: delete from attribute where master_id=? and attribute_type_id=? 
Hibernate: insert into attribute (master_id, attribute_type_id, value) values (?, ?, ?) 
Hibernate: insert into attribute (master_id, attribute_type_id, value) values (?, ?, ?) 

爲什麼發生這種情況?如何預防它?

回答

4

根據Hibernate Reference

因爲Set的結構,Hibernate並不當一個元件被「變爲」更新的行。對Set的更改始終通過INSERT和DELETE各個行來工作。

如果Hibernate試圖更新您的,即使你不修改它集,也許在Attribute類的equalshashcode實現被打破?

+0

這工作,非常感謝。 – 2010-07-27 10:10:24