views:

42

answers:

2

Hello guys im trying to hydrate a DTO using projections in NHibernate this is my code

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

this code is throwing an exception? anyone know what is the problem?

here is the error message

Message: could not resolve property: Patient.Name.Fullname of: OneCare.Domain.Entities.Patient

+1  A: 

You have to create a join to your Parent.Name property.

So try before setting the projections to create in alias to your Patient.Name property

e.q.

IList<PatientListViewModel> list = 
                                y.CreateCriteria<Patient>()
                                 .CreateAlias("Name", "name")

                                        .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("name.Fullname"), "Fullname")     
                           )

Sorry I did not check this, as all depend on your entities classes. But the idea is that you have to create an alias. If you can not fix the issue, please provide the your classes.

Updated!

I've created two entities, Patient and Doctor:

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

Next the repository contains only yours query translated to Criteria API

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

And here is the unittest and the result of the query

    [Test]
    public void CanGetPatients()
    {
        var repository = new PatientRepository();
        repository.GetPatientsForDoctor(1L);
    }

and result is:

 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

As I said you need just to create an Alias and join tables between them. But I think, that using HQL is more plausible in this case. Use criteria only you have dynamic queries. As you can see, the criteria one select all fields which could create a performance lack. Of couse you are working with simple things, but in real application be very carefully with generated queries.

Have a nice day!

isuruceanu
check my answer comment box is too limited for my post. :)
reggieboyYEAH
A: 

i achieve what i want using this HQL query

        IList<PatientListViewModel> list =
                           _patientDao.ContextSession
                           .CreateQuery(@"
                                                 select 
                                                                f.Birthdate         as Birthdate,
                                                                f.Doctor.Id         as DoctorId,
                                                                f.Gender            as Gender,
                                                                f.Id                as PatientId,
                                                                f.Name              as Name                                                                    
                                                 from Patient f
                                                 where f.Doctor.Id = :userid
                                         ")
                                         .SetInt64(@"userid",WebUserHelper.CurrentUserId)
                     .SetResultTransformer(NHibernate.Transform.Transformers.AliasToBean<PatientListViewModel>())
                     .List<PatientListViewModel>();

now im just wondering if this is possible using the criteria api, Thanks in advance

reggieboyYEAH