Inner or Right Outer Join in Nhibernate and Fluent Nhibernate on Many to Many collection

前端 未结 2 993
情深已故
情深已故 2021-01-05 12:45

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

2条回答
  •  被撕碎了的回忆
    2021-01-05 13:14

    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.

提交回复
热议问题