Hibernate-从集合中删除物品
我想阐明我做过的假设,即Hibernate 的功能。假设我有一个a
的类,@onetomany
用b
's映射。这些b每个都有一个a
父母的引用,以支持mappedby
属性
。 ,Hibernate是否有能力自动将B
?的父字段自动取消。从我所做的所有测试中,从集合中删除某些内容时,它实际上并未通过更改子女中的父介绍来更新数据库。
I wanted to clarify an assumption that I have made, regarding the functionality of Hibernate
. Assume I have a Class of A
's with a @OneToMany
mapping with B
's. These B's each have an A
parent reference to support a mappedBy
attribute on A.
When I remove a B
from the collection in A
, does hibernate have the ability to automatically null out the parent field inside of B
?. From all of the tests I have done, when removing something from a collection, it doesn't actually update the database by changing the parent reference in the child.
This link seems to support my claim as they manually null out the parent reference, along with removing it from the parent Set
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每当从两端映射关联时,这些末端之一就被指定为主动,而一个被动端。 (被动端是使用
mappedby =
或inverse =“ true”
的映射的末端)hibernate却没有,也不会在活动活动中更新集合的被动端结束已更改,因为它只能在冲洗时间检测到此类更改。因此,调用代码修改关联的两端是一个好实践,以确保对象模型始终处于一致的状态。
Hibernate本身并不关心关联的两个端是否一致,因为它仅在冲洗到数据库时查看活动端。
在映射一对多协会时,应指定一对一的终点。您链接的冬眠手册中的部分尝试解释原因。
Whenever an association is mapped from both ends, one of these ends is designated the active, and one the passive end. (The passive end is the one mapped using
mappedBy=
orinverse="true"
)Hibernate does not, and can not, update the passive end of a collection when the active end is changed, as it can detect such changes only at flush time. Hence it is considered good practice for calling code to modify both ends of the association, to ensure the object model is always in a consistent state.
Hibernate itself does not care whether the two ends of the association are consistent, as it only looks at the active end when flushing to the database.
When mapping a one-to-many association, the one-to-many end should be designated passive. The section from the hibernate manual you link to attempts to explain why.