I have an issue in NHibernate regarding a left join using "JoinAlias" when the result query SQL that I am looking for is this :
"select * from EntityA T1 left join EntityB T2 on T2.EntityAId=T1.id"
And in NHibernate I have this but doesn't work:
var query = _session.QueryOver(() => EntityA)
.Left.JoinAlias(() => EntityA, () => EntityB.EntityA)
In NHibernate EntityA
doesn't reference to EntityB
but EntityB
as a reference to EntityA
.
public class EntityA
{
public int Id {get;set;}
}
public class EntityB
{
public int Id {get;set;}
public EntityA EntityA {get;set;}
}
How can I make this very simple left join in HHibernate Work?
This is not possible with
Criteria
orQueryOver
. But we can use HQL, which does support thatSo in the case above we would have HQL like this:
Almost the same issue
But in case described above, there is reversed relation, already mapped. And that means, that we can extend the C# entity definitions:
Having that in place, we can use standard
QueryOver
API: