2011-11-04 3 views
0

J'ai le problème suivant avec une requête NHibernate.Nhibernate joindre des tables par plusieurs colonnes

une table

LearnerDocuments : 
    LearnerNumber 
    CandidateNumber 
    .... 

autre table

LearnerRegistration : 
    LearnerId 
    LearnerNumber 
    .... 

le dernier

Learner : 
    LearnerId 
    LearnerName 
    .... 

La relation entre LearnerDocuments et et LearnerRegistration n'existe pas. Comment puis-je écrire une requête qui obtient tous LearnerRegistration qui ont LearnerDocuments?

+0

Pouvez-vous nous dire quelle technologie de cartographie utilisez-vous? Comme automapping ou hbm ou fluent – Thanigainathan

+0

Vous devrez définir la relation/mapping pour 'joindre 'la table. NH ne peut pas faire de jointures ad hoc. Cependant, vous pouvez faire une clause 'exists'. Lequel veux-tu? – dotjoe

+0

le problème a été résolu en utilisant Projections.Exist – JeneaCr

Répondre

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));