Inner join fetch in Linq query
Is it possibl开发者_StackOverflow中文版e to modify a query (or the mapping) so that the call to the .Fetch()
extension method of NHibernate results in a inner join in the generated SQL query?
In HQL this is easy, you just have to write inner join fetch
instead of left join fetch
, but I couldn't find that for Linq.
Since this isn't possible yet, I have created a Jira issue for it: NH-2790
UPDATE 2:
https://www.nuget.org/packages/NHibernate.Linq.InnerJoinFetch
Enjoy!!!
UPDATE:
I finally managed to do this and sent the followign pull request to the NH team
https://github.com/nhibernate/nhibernate-core/pull/210
In Fact, it is possible but you need to hack NHibernate, the line to change is this one
tree.AddFromClause(tree.TreeBuilder.LeftFetchJoin(join, tree.TreeBuilder.Alias(alias)));
into
tree.AddFromClause(tree.TreeBuilder.FetchJoin(join, tree.TreeBuilder.Alias(alias)));
at the ProcessFetch.cs file
It seems the behavior is hardcoded for Linq and I think its because its using the extension methods in order to send what to use for the DefaultQueryProvider and re-linq processing, I still need to figure out how to specify the kind of join you want to do on eager fetching using the linq api but I'm working on it, hopefully I'll send a pull request to the NH team so they can put this into the codebase, for now in my project I'll use my custom NH version
Not sure if you can control the JOIN type in LINQ but you can still have strongly typed queries controlling the JOIN type using the QueryOver API.
var ordersWithCustomers = Session.QueryOver<Order>()
.JoinQueryOver(o => o.Customer,JoinType.InnerJoin) //You can specify join type
.List();
Also try adding not-null="true"
in your mapping, although I just tried this and it didn't make a difference to the join type in the query.
Wierdly this works for the inner join (using the old ANSI SQL joining syntax) but doesn't fetch the Customer.
var ordersWithCustomers =
from o in Session.Query<Order>()
join c in Session.Query<Customer>() on o.Customer.Id equals c.Id
select o;
精彩评论