单向父子关联不为空
我有一个类似于PurchaseOrder(父级)和PurchaseOrderLine(子级)模式的类结构,其中订单行只能通过保存父级PurchaseOrder 保存到数据库中,并且也只能通过父级访问。
数据库已将PurchaseOrderLine.PurchaseOrder 设置为不允许空值。
从网上搜索看来,当子项的 PurchasingOrder 列具有 NOT NULL 约束时,如果行上没有指向回的属性,则不可能通过 IList 属性与 PurchaseOrder 进行单向关联。
事实真的如此吗?这似乎是人们想要用 ORM 做的最基本的事情之一,我发现很难接受像 NHibernate 这样成熟的产品无法处理这样的基本场景。
I have a class structure which is akin to a PurchaseOrder (parent) and PurchaseOrderLine (child) pattern, where the order lines will only be saved to the DB by saving the parent PurchaseOrder and will only ever be accessed via the parent too.
The DB has PurchaseOrderLine.PurchaseOrder set to not permit null values.
It seems from searching through the web that it is not possible to have a uni-directional association from PurchaseOrder via an IList property without having to have a property on the line pointing back when the child has a NOT NULL constraint for its PurchaseOrder column.
Is this really the case? It seems like one of the most basic things one would want to do with an ORM, I find it difficult to accept that a product as mature as NHibernate cannot handle such a basic scenario.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
现在可以使用 Not.KeyNullable() 在 NH3 中实现这一点
This can now be achieved in NH3 using the Not.KeyNullable()
不,事实并非如此。请参阅此问题的答案中提供的示例: 何时在 NHibernate / Hibernate OneToMany 关系上使用 inverse=false?
No it's not the case. Please see the example provided in the answer to this question: When to use inverse=false on NHibernate / Hibernate OneToMany relationships?
好吧,可能是这样的情况,您不能仅在一端定义单向一对多关系,但我会反对你的说法,即这是“人们想要使用 ORM 做的最基本的事情之一”。
最基本的事情之一是仅在多端定义单向一对多 - 因为它对于 RDBM 表来说是自然的 。 ORM(尽管存在常见的误解)无意(或能够)从底层数据源完全抽象域模型。即使在某些情况下可以,数据库端也会遇到选择 N+1 问题或非常无效的查询。
在一侧定义一对多给人的印象是,即对集合进行计数是很便宜的。普通对象图就是这种情况,但 NHibernate 实体则不然,因为读取集合会导致(至少一次)调用数据库。从一侧进行急切提取也无法按照预期的方式正确使用数据库连接机制(与从多侧进行急切提取相反)。
即使我不同意很多论点,我认为阅读一些说“ORM 是一种反模式”的文章是有用的,例如 这个。它们帮助我利用我对 ORM 的思考方式,并让我将 ORM 视为两种不匹配范式之间的折衷方案,而不是将一个范式隐藏在另一个范式背后的方式。
Well, it may be the case that you can't have unidirectional one-to-many relationship defined only on one side, but I'll argue with your statement that this is "one of the most basic things one would want to do with an ORM".
One of the most basic things would be to have unidirectional one-to-many defined only on many side - as it is natural for RDBM tables. And ORMs (despite the common misconception) are not intended (or able) to fully abstract domain model from underlying data source. Even if in some cases they can, the database side suffers from select N+1 problems or very ineffective queries.
Defining one-to-many at one side makes an impression that i.e. counting the collection is cheap. It is the case with plain object graphs, but not with NHibernate entities, as reading collection causes (at least one) call to the database. Eager fetching from one side is also not able to properly use database join mechanism in the way it's intended to be used (opposite to eager fetch from many side).
Even if I don't agree with a lot of arguments, I think it is useful to read some of the articles saying that "ORM is an anti-pattern", like this one. They helped me to leverage the way I think about ORMs and make me think about ORMs as a compromise between two not matching paradigms, but not the way to hide one behind another.