gpt4 book ai didi

java - Hibernate:复合元素导致每次提交时删除+插入

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:28:24 24 4
gpt4 key购买 nike

这是我的映射的一部分:

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

为什么会这样?如何预防?

最佳答案

根据 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.

如果 Hibernate 尝试更新您的集合,即使您不修改它,也许您在 Attribute 类中的 equalshashcode 实现是坏了?

关于java - Hibernate:复合元素导致每次提交时删除+插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3342149/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com