内或右外在多对多集合中加入 Nhibernate 和 Fluent Nhibernate [英] Inner or Right Outer Join in Nhibernate and Fluent Nhibernate on Many to Many collection

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

问题描述

如何强制 NHibernate 在多对多集合上执行右外连接或内连接而不是左外连接?

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?

我想要这样做的原因是因为过滤应用于集合元素.使用左连接,您将获得与未过滤查询返回的行数相同的行数,但过滤掉的元素只显示所有字段的 NULL.但是,使用右联接,将从查询中返回正确数量的行和元素.

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

推荐答案

我认为在集合映射中指定右联接或内联接是不可能的.fetch 子句的唯一选项是默认的左外连接和顺序选择.

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.

问题在于,当您创建映射时,NHibernate 需要知道如何从连接的左侧获取任意根项的集合元素.使用右连接或内连接时,根对象可能不存在于返回的集合中,因此您会陷入困境.

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.

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

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.

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

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

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