问题描述
<$>我在NHibernate中遇到了关于使用JoinAlias的左连接的问题。 c $ c>select * from EntityA T1 left join join EntityB T2 on T2.EntityAId = T1.id
$ $ $ $ $ $ $ $ $ $ var query = _session.QueryOver(()=> EntityA)
.Left.JoinAlias(()=> EntityA,()=> EntityB.EntityA)
在NHibernate中 c 或 QueryOver 是不可能的。但是,我们可以使用 HQL ,它支持
- (小引用和片段)
来自公式,参数
来自公式形式,参数为参数
所以在上面的例子中我们会有这样的HQL:
FROM EntityA T1
,EntityB T2
WHEERE T2.EntityAId = T1.id
几乎是
但是在上面描述的情况下,存在着已经映射的反转关系。这意味着,我们可以扩展C#实体定义:
public class EntityA
{
public int Id {get; set;}
//一对多
//以下关系的双向映射
public IList< EntityB> EntityBColl {get;组; }
}
public class EntityB
{
public int Id {get; set;}
//多对一
//这是实际上双向映射的反转端
public EntityA EntityA {get; set;}
}
具备这个功能,我们可以使用标准的 QueryOver API:
//别名
EntityA EntityA = null;
EntityB EntityB = null;
// query
var query = session.QueryOver(()=> EntityA)
.Left.JoinAlias(()=&EntityA.EntityBColl,()= >实体B)
...
;
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 or QueryOver. But we can use HQL, which does support that
- 14.2. The from clause (small cite and snippet)
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) ... ;
这篇关于Nhibernate QueryOver JoinAlias UnRelated Entity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!