什么时候可以在 NHibernate 中使用单向关系?
我正在尝试移植大型 .NET 实体图以使用 NHibernate,但我遇到了一个问题,即大多数关系仅是单向定义的 - 在大多数情况下,子类包含对父类的引用,但父级不包含其子级的引用集合。添加所有集合以将关系转变为双向关系将需要大量工作,因此我想知道如果不这样做,NHibernate 会产生什么后果?
我注意到的一个后果是级联删除似乎失败(子级没有在数据库中删除,导致引用完整性违规)。这是唯一的后果还是还有其他我需要注意的问题?
对于关系何时应该是单向的还是双向的,是否有任何指导原则?
谢谢
I'm trying to port a large graph of .NET entities to use NHibernate, but I'm encountering an issue that most of the relationships are only defined unidirectionally - in most cases, the child class contains a reference to the parent, but the parent does not contain the collection of refs to its children. It would be quite a bit of work to add all the collections to turn the relationships into bidirectional ones, so I'm wondering what the consequences for NHibernate would be of not doing so?
One consequence I've noticed is that cascading deletes seem to fail (child doesn't get deleted in the DB, causing a referential integrity violation). Is that the only consequence or are there other issues I need to be aware of?
Are there any guidelines for when relationships should be uni or bi-directional?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为无法级联删除将是 NHibernate 本身的唯一问题。
但您将无法轻松地浏览该图。你可以从孩子到父母,但显然不能从父母到孩子。因此,每次您想要父级的所有子级时,您都必须发出查询。
因此,如果您将 NH 用于持久域模型,其中您有一个根对象,您需要使用其中的子对象来执行某些操作,则必须从模型内发出查询来获取子对象。因此,您的模型将与您的数据访问耦合。
或者,您必须将子对象作为集合传递给父对象,但随后可能很容易让模型上的集合开始宽度,以便 NH 可以为您填充它们。
I think that not being able to cascade the deletes will be the only issue with NHibernate per se.
But you will not be able to easily walk the graph. You can do it from child to parent, but obviously not from parent to child. So you would have to issue a query each time you want all the childs from a parent.
So if you are using NH for a persisted domain model, where you have a root object from which you need to use the child objects for certain operations, you would have to issue queries from within the model to get the children. So your model will be coupled to your data access.
Or you would have to pass the children to the parent object as collections, but then it might be just at easy to have the collections on the model to begin width so NH could fill them for you.