Hibernate:composite-element在每次提交时都会导致delete + insert

时间:2010-07-27 09:13:37

标签: java hibernate

这是我的映射的一部分:

<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集,没有任何更新,Hibernate仍会在提交时执行一批deleteinsert操作。

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

为什么会这样?怎么预防呢?

1 个答案:

答案 0 :(得分:4)

根据Hibernate Reference

  

由于Set的结构,当元素被“更改”时,Hibernate不会更新行。对Set的更改始终通过单个行的INSERT和DELETE工作。

如果Hibernate尝试更新您的设置即使您没有修改它,那么equals课程中的hashcodeAttribute实施可能会被破坏吗?

相关问题