以正确的方式使用 NHibernate 更新实体集合
关于 NHibernate 如何工作的一个简单问题:
我有一个父实体,其中包含其他子实体的集合。这些子实体具有对其所属父实体的引用。现在我想向父实体实现一个 Add 方法,以向其添加一个子实体。该 Add 方法是否应该仅将子级添加到其新的父级集合中,或者还应该更新子级的父级引用,或者还应该从其先前的父级集合中删除添加的实体?我是否必须以该方法完成所有这些事情,或者 NHibernate 会为我做一些事情吗?
谢谢。
A simple question regarding how NHibernate works:
I have a parent entity that has a collection of other child entities. Those child entities have a reference to the parent entity they belong to. Now I want to implement an Add method to the parent entity that would add a child to it. Should that Add method only add the child to its new parents collection, or should it also update the parent reference of the child or should it also remove the added entity from its previous parents collection? Do I have to do all these things in that method or will NHibernate do something for me?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您绝对应该确保这种关系对于所有相关方来说都是“有效的”。
确保父级在其
children
集合中拥有子级,并且子级在其parent
引用中拥有父级,并且子级已从其旧中删除>parent
(如果有的话)。最后一步是唯一我不确定是否强制执行的一步。You should definitely make sure that the relationship is "valid" for all parties involved.
Make sure that the parent has the child in its
children
collection, and the the child has the parent in itsparent
reference, and that the child is removed from its oldparent
(if it had one). The last step is the only one for which I'm not sure it's mandatory.