【发布时间】:2014-01-08 02:31:48
【问题描述】:
Hibernate document 表示如果我想使用列表,那么我需要为update="false" and insert="false" 设置属性。
请告诉我为什么需要这些属性以及这有什么用处?
如果你使用 List 或其他索引集合,请将 key 列设置为 外键不为空。 Hibernate 将管理关联 从集合端维护每个元素的索引, 通过设置 update="false" 和 插入=“假”:
<class name="Person">
<id name="id"/>
...
<many-to-one name="address"
column="addressId"
not-null="true"
insert="false"
update="false"/>
</class>
<class name="Address">
<id name="id"/>
...
<list name="people">
<key column="addressId" not-null="true"/>
<list-index column="peopleIdx"/>
<one-to-many class="Person"/>
</list>
</class>
我也浏览过这篇帖子Setting update and insert property in Hibernate,但是当我编写一个简单的程序来创建和保存我的 Person 和 Address 对象时,我可以看到 addressId 属性是由 hibernate 本身插入和更新的:
Hibernate: insert into Address (addressId) values (?)
Hibernate: insert into person1 (addressId, peopleId, personId) values (?, ?, ?)
Hibernate: insert into person1 (addressId, peopleId, personId) values (?, ?, ?)
09:19:08,526 DEBUG AbstractCollectionPersister:1205 - Inserting collection: [partc.onetomany1.Address.people#156]
Hibernate: update person1 set addressId=?, peopleId=? where personId=?
Hibernate: update person1 set addressId=?, peopleId=? where personId=?
【问题讨论】: