开发者

Hibernate: composite-element results in delete+insert on every commit

Here's a part of my mapping:

<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>

If I simply scan the attributes set, without any updates, Hibernate would still execute a batch of delete and insert operations on commit.

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开发者_如何学JAVA attribute (master_id, attribute_type_id, value) values (?, ?, ?)
Hibernate: insert into attribute (master_id, attribute_type_id, value) values (?, ?, ?)

Why is it happening? How to prevent it?


According to Hibernate Reference

Because of the structure of a Set, Hibernate does not UPDATE a row when an element is "changed". Changes to a Set always work via INSERT and DELETE of individual rows.

If Hibernate tries to update your set even if you don't modify it, maybe your equals and hashcode implementations in Attribute class are broken?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜