Nhibernate 中的内部或右外部连接以及多对多集合上的 Fluent Nhibernate
如何强制 NHibernate 在多对多集合上执行 RIGHT 外连接或 INNER 连接而不是 LEFT 外连接?
我想要这样做的原因是因为过滤应用于集合元素。使用左联接,您将获得与未过滤查询返回的行数相同的行数,但过滤掉的元素仅在所有字段中显示 NULL。但是,通过右连接,查询会返回正确的行数和元素数。
我希望人们可以在集合映射中的某处指定连接。
How can I force NHibernate to do a RIGHT outer join or an INNER join instead of a LEFT outer join on a many to many collection?
The reason I would want to do this is because of filtering applied to the collection elements. With a left join, you get the same number of rows returned as an unfiltered query, but the elements that are filtered out just show NULL for all of the fields. However, with a right join, the correct number of rows and elements are returned from the query.
I would expect that one could specify the join somewhere in the mapping of the collection..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为不可能在集合映射中指定右连接或内部连接。带有
fetch
子句的唯一选项是默认的左外连接和顺序选择。问题是,当您创建映射时,NHibernate 需要知道如何从连接的左侧获取任意根项的集合元素。使用右连接或内连接,根对象可能不存在于返回的集合中,因此您会陷入困境。
如果过滤条件是静态的,您可以在映射中指定
where
子句。我认为这将是适合您情况的推荐解决方案。解决方法是使集合在对象中私有,然后创建另一个调用 HQL 查询的属性来实现内部联接并返回该集合。返回的集合将具有您想要的语义,但您需要单独的方法来添加或删除集合中的项目。
I don't think it's possible to specify a right or inner join in the collection mapping. The only options with the
fetch
clause are the default left outer join and sequential select.The problem is that when you create a mapping, NHibernate needs to know how to fetch the collection elements for any arbitrary root item from the left side of the join. With a right or inner join, the root object may not exist in the returned collection, so you're stuck at that point.
If the filter criteria is static, you can specify a
where
clause in the mapping. I think this would be the recommended solution for your situation.A workaround would be to make the collection private in your object, then create another property that calls an HQL query to implement the inner join and returns that collection. This returned collection would have the semantics you want, but you'll need separate methods to add or remove items from the collection.
您可以使用 NHibernate 的 HQL 语法来生成类似于 SQL 的查询,但使用 NHibernate 的映射功能。 HQL 支持
右外连接
(或简称为右连接
)。以下页面是 NHibernate 的 HQL 查询语言的良好参考:You can use NHibernate's HQL syntax to generate a query that's reminiscent of SQL, but uses NHibernate's mapping abilities. HQL supports
right outer join
(or justright join
for short). The following pages are good references for NHibernate's HQL query language: