2010-03-01 5 views
0

J'ai la requête ICriteria suivante,NHibernate générer SQL correcte, mais le retour des lignes vides

public override IQueryable<ApplicationVehicleSummary> GetQuery(ISession session) 
    { 
     ICriteria results = session.CreateCriteria<Part>() 
      .Add(Restrictions.Eq("PartNumber", _partNumber)) 
      .CreateCriteria("Applications") 
      .CreateAlias("Vehicles", "vehicle", global::NHibernate.SqlCommand.JoinType.InnerJoin) 
      .SetProjection(Projections.Property("vehicle.Make"), 
      Projections.Property("vehicle.Model"), 
      Projections.Property("vehicle.Type"), 
      Projections.Property("vehicle.Engine"), 
      Projections.Property("vehicle.ProductionStart"), 
      Projections.Property("vehicle.ProductionEnd")) 
      .SetResultTransformer(Transformers.AliasToBean<ApplicationVehicleSummary>()); 

     return results.List<ApplicationVehicleSummary>().AsQueryable(); 
    } 

et produit l'instruction SQL suivante.

SELECT vehicle2_.Make   as y0_, 
    vehicle2_.Model   as y1_, 
    vehicle2_.Type   as y2_, 
    vehicle2_.Engine   as y3_, 
    vehicle2_.ProductionStart as y4_, 
    vehicle2_.ProductionEnd as y5_ 
FROM Parts this_ 
    inner join Applications applicatio1_ 
    on this_.PartId = applicatio1_.PartId 
    inner join VehiclesToApplications vehicles5_ 
    on applicatio1_.ApplicationId = vehicles5_.ApplicationId 
    inner join Vehicles vehicle2_ 
    on vehicles5_.VehicleId = vehicle2_.VehicleId 
WHERE this_.PartNumber = '0500-252' /* @p0 */ 

Quand je lance la requête de mon application NHibernate retourne la bonne quantité de lignes, mais tous les champs sont nuls ou vides. Mais quand je prends le SQL qu'il a généré (à partir de nhibernate profiler) et l'exécute sur ma base de données, il renvoie les résultats corrects.

Qu'est-ce que je fais de mal/manque?

+0

Voir [cette question] (http: // stackoverflow.com/questions/2146637/return-entity-via-projection-query). –

Répondre

0

Soit utiliser les mêmes noms comme champs de ou préciser la cartographie explicitement:

.setProjection(Projections.projectionList() 
       .add(Projections.property("vehicle.Model"), "fieldName1") 
       .add(Projections.property("vehicle.Type"), "fieldName2") 

ou utiliser des attributs

également vérifier ce projet Fluent nHibernate

+0

Cela a bien fonctionné. J'ai été jeté dans la partie profonde avec nHibernate, mais avec un peu d'aide de votre part, je commence à m'y habituer. Merci, –

Questions connexes