Hibernate-从集合中删除物品

发布于 2024-12-05 06:47:42 字数 526 浏览 1 评论 0原文

我想阐明我做过的假设,即Hibernate 的功能。假设我有一个a的类,@onetomanyb's映射。这些b每个都有一个a父母的引用,以支持mappedby属性

。 ,Hibernate是否有能力自动将B?的父字段自动取消。从我所做的所有测试中,从集合中删除某些内容时,它实际上并未通过更改子女中的父介绍来更新数据库。

此链接>似乎支持我的主张,因为他们手动将父母的参考删除,并从父set中删除。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

攀登最高峰 2024-12-12 06:47:42

每当从两端映射关联时,这些末端之一就被指定为主动,而一个被动端。 (被动端是使用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= or inverse="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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文