Nhibernate QueryOver JoinAlias UnRelated Entity

♀尐吖头ヾ 提交于 2019-12-02 04:42:41
Radim Köhler

This is not possible with Criteria or QueryOver. But we can use HQL, which does support that

Multiple classes may appear, resulting in a cartesian product or "cross" join.

from Formula, Parameter
from Formula as form, Parameter as param

So in the case above we would have HQL like this:

FROM EntityA T1 
   , EntityB T2 
WHEERE T2.EntityAId = T1.id

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:

public class EntityA
{ 
   public int Id {get;set;}
   // one-to-many
   // the bidirectional mapping of the below relation
   public IList<EntityB> EntityBColl { get; set; }
}

public class EntityB
{ 
   public int Id {get;set;}
   // many-to-one
   // this is the inversed end in fact of the bidirectional mapping
   public EntityA EntityA {get;set;}
}

Having that in place, we can use standard QueryOver API:

// aliases
EntityA EntityA = null;
EntityB EntityB = null;

// query
var query = session.QueryOver(() => EntityA)
    .Left.JoinAlias(() => EntityA.EntityBColl , () => EntityB)
    ...
    ;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!