NHibernate投影组件

NHibernate投影组件,nhibernate,Nhibernate,大家好,我正在尝试使用NHibernate中的投影水合DTO这是我的代码 IList<PatientListViewModel> list = y.CreateCriteria<Patient>() .SetProjection(Projectio

大家好,我正在尝试使用NHibernate中的投影水合DTO这是我的代码

IList<PatientListViewModel> list = 
                                    y.CreateCriteria<Patient>()                                 
                                            .SetProjection(Projections.ProjectionList()
                                            .Add(Projections.Property("Birthdate"), "Birthdate")
                                            .Add(Projections.Property("Doctor.Id"), "DoctorId")
                                            .Add(Projections.Property("Gender"), "Gender")
                                            .Add(Projections.Property("Id"), "PatientId")
                                            .Add(Projections.Property("Patient.Name.Fullname"), "Fullname")     
                               )
                         .SetResultTransformer(Transformers.AliasToBean<PatientListViewModel>())
                         .List<PatientListViewModel>();  

您必须创建与父对象的连接。Name属性

因此,请在将要在alias中创建的投影设置为Patient.Name属性之前进行尝试

e、 问题

接下来,存储库只包含您的查询,该查询被转换为CriteriaAPI

public IList<Patient> GetPatientsForDoctor(long doctorId)
    {
        return this.Session.CreateCriteria(typeof(Patient), "patient")
            .CreateAlias("patient.Doctor", "doc")
            .Add(Restrictions.Eq("doc.Id", doctorId))
            .List<Patient>()
        ;
    }
结果是:

 NHibernate: SELECT this_.patientId as patientId70_1_, this_.birthDate as birthDate70_1_, this_.gender as gender70_1_, 
this_.name as name70_1_, this_.deletedDate as deletedD5_70_1_, this_.doctorId as doctorId70_1_, 
this_.deletedById as deletedB7_70_1_, doc1_.doctorId as doctorId71_0_, doc1_.name as name71_0_, 
doc1_.deletedDate as deletedD3_71_0_, doc1_.deletedById as deletedB4_71_0_ 
FROM Patients this_ 
inner join Doctors doc1_ on this_.doctorId=doc1_.doctorId 
WHERE doc1_.doctorId = @p0;@p0 = 1
正如我所说的,您只需要创建一个别名并在它们之间连接表。 但我认为,在这种情况下,使用HQL更合理。仅当您有动态查询时才使用条件。正如您所看到的,标准选择了可能导致性能不足的所有字段。当然,您处理的是简单的事情,但在实际应用中,必须非常小心地处理生成的查询


祝你今天愉快

选中我的答案评论框对我的帖子来说太有限了。:)
 public class Patient : AdvanceEntity
{
    public virtual DateTime BirthDate { get; set; }

    public virtual Doctor Doctor { get; set; }

    public virtual int Gender { get; set; }

    public virtual string Name { get; set; }
}

public class Doctor : AdvanceEntity
{
    public virtual string Name { get; set; }
}
public IList<Patient> GetPatientsForDoctor(long doctorId)
    {
        return this.Session.CreateCriteria(typeof(Patient), "patient")
            .CreateAlias("patient.Doctor", "doc")
            .Add(Restrictions.Eq("doc.Id", doctorId))
            .List<Patient>()
        ;
    }
    [Test]
    public void CanGetPatients()
    {
        var repository = new PatientRepository();
        repository.GetPatientsForDoctor(1L);
    }
 NHibernate: SELECT this_.patientId as patientId70_1_, this_.birthDate as birthDate70_1_, this_.gender as gender70_1_, 
this_.name as name70_1_, this_.deletedDate as deletedD5_70_1_, this_.doctorId as doctorId70_1_, 
this_.deletedById as deletedB7_70_1_, doc1_.doctorId as doctorId71_0_, doc1_.name as name71_0_, 
doc1_.deletedDate as deletedD3_71_0_, doc1_.deletedById as deletedB4_71_0_ 
FROM Patients this_ 
inner join Doctors doc1_ on this_.doctorId=doc1_.doctorId 
WHERE doc1_.doctorId = @p0;@p0 = 1