内部或外部加入Nhibernate和流利Nhibernate在多对多收集 [英] Inner or Right Outer Join in Nhibernate and Fluent Nhibernate on Many to Many collection

查看:109
本文介绍了内部或外部加入Nhibernate和流利Nhibernate在多对多收集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何强迫NHibernate在多对多的集合上进行右外连接或INNER连接而不是左连接?



想要这样做是因为过滤应用到了收集元素。使用左连接时,返回的行数与未过滤的查询数相同,但过滤出的元素对所有字段都显示为NULL。但是,使用正确的连接,从查询中返回正确数量的行和元素。



我希望可以指定连接的某个地方我不认为有可能在集合映射中指定正确的或内部的连接。 fetch 子句的唯一选项是默认的左外部连接和顺序选择。



问题是,当你创建一个映射,NHibernate需要知道如何从连接的左侧获取任意任意根项目的集合元素。使用右连接或内连接,根对象可能不存在于返回的集合中,所以你被困在这一点。



如果过滤条件是静态的,可以在映射中指定 where 子句。我认为这将是您的情况推荐的解决方案。

解决方法是将集合私有在您的对象,然后创建另一个属性,调用HQL查询实现内部连接并返回该集合。这个返回的集合将具有所需的语义,但是您需要单独的方法来添加或删除集合中的项目。


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..

解决方案

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和流利Nhibernate在多对多收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆