2011-11-04 53 views
0

NHibernate查询有以下问题。Nhibernate加入多列表

一个表

LearnerDocuments : 
    LearnerNumber 
    CandidateNumber 
    .... 

其他表

LearnerRegistration : 
    LearnerId 
    LearnerNumber 
    .... 

LearnerDocuments和与LearnerRegistration之间的最后一个

Learner : 
    LearnerId 
    LearnerName 
    .... 

的关系不存在。

我该如何写一个查询,获取所有LearnerRegistration哪些有LearnerDocuments

+0

您能否告诉您正在使用哪种测绘技术?像automapping或hbm或fluent – Thanigainathan

+0

您需要定义关系/映射到'join“表。 NH不能做adhoc连接。但是你可以做一个'存在'条款。你要哪个? – dotjoe

+0

使用Projections.Exist解决了该问题 – JeneaCr

回答

0
criteria.CreateCriteria(typeof(LearnerRegistration), "av") 
        ...... 

        .Add(Restrictions.IsNotNull("RegistrationNumber"))      
        .Add(Subqueries.PropertyIn("l.Id", detachedCriteria)) 

       ; 


      var candidate = DetachedCriteria.For<LearnerDocuments>("sd") 
         .SetProjection(Projections.ProjectionList() 
         .Add(Projections.Property("sd.CandidateNumber")) 
         .Add(Projections.Property("sd.RegistrationNumber"))) 
         .Add(Restrictions.EqProperty("sd.CandidateNumber", "l.LearnerNumber") 
         && Restrictions.EqProperty("sd.RegistrationNumber", "lr.RegistrationNumber") 
         ); 

      var proj = Projections.Conditional(Subqueries.Exists(candidate),Projections.Constant(true),Projections.Constant(false));