NHibernate version: 1.2.0 GA
I have two mapped classes, let's call them Parent and Child, where Parent has a simple many-to-one mapping to Child. I want to load those using a native SQL query, so I use the following code:
ISQLQuery query = Session.CreateSQLQuery(@"
SELECT p.*, c.*
FROM Parent p
INNER JOIN Child c ON p.childId = c.[id]
(more joins and criteria)
";
query.SetFlushMode(FlushMode.Never);
query.AddEntity("p", typeof(Parent));
query.AddJoin("c", "p.Child");
This works, but using SQL Profiler I can see that it's doing a separate query to load each Child instance, even though they were all already loaded in the original query. After some debugging, it turns out that when NHibernate goes to look for p.Child in the session it doesn't find it, so it loads it again! Simply swapping around the selects fixes the problem:
ISQLQuery query = Session.CreateSQLQuery(@"
SELECT c.*, p.*
FROM Parent p
INNER JOIN Child c ON p.childId = c.[id]
(more joins and criteria)
";
Could this be fixed so that NHibernate is smart enough to realise it has the child it needs in the query results, regardless of the order of the select list?
|