你必須創建一個連接到您的Parent.Name財產。
所以嘗試設置投影別名創建您Patient.Name財產
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")
)
對不起,我沒有檢查這一點,因爲所有這些都取決於你的實體類。但是這個想法是你必須創建一個別名。 如果您無法解決問題,請提供您的課程。
更新!
我創建了兩個實體,病人和醫生:
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; }
}
下一頁庫只包含你的查詢轉換爲標準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>()
;
}
這裏是單元測試和的結果查詢
[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
正如我所說的,你只需要創建一個別名並在它們之間連接表。 但我認爲,在這種情況下使用HQL更合理。使用標準只有你有動態查詢。正如你所看到的,選擇所有可能導致性能缺失的領域的標準。你正在處理簡單的事情,但在實際應用中要非常小心地生成查詢。
祝您有美好的一天!
檢查我的答案評論框太限我的帖子。 :) – reggieboyYEAH 2010-06-15 14:36:57