nHibernate生成正确的SQL,但返回空行

时间:2010-03-01 16:17:04

标签: c# nhibernate

我有以下ICriteria查询,

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

它产生以下SQL。

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 */

当我从我的应用程序运行查询时,nHibernate会返回正确数量的行,但所有字段都为null或为空。但是,当我使用它生成的SQL(来自nhibernate profiler)并在我的数据库上运行它时,它返回正确的结果。

我做错了什么/失踪了?

1 个答案:

答案 0 :(得分:0)

使用相同的名称作为字段,或明确指定映射:

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

或使用属性

还要检查此项目Fluent nHibernate