我正在开发一个转换项目.net 2.0到.net 4.5.1,并且遇到了NHibernate HQL的问题。我的问题是在运行时添加的子查询,该查询存储在数据库中以进行过滤,并在NH进行查询之前附加到HQL。子查询中的两个左连接将被删除。
翻译前的查询:
Select allergy From Allergy allergy left join allergy.HealthcareWorker healthcareWorker where (1=1) and healthcareWorker.Id = :healthcareWorkerId and ((
allergy.Id in ( Select allergy.id From Allergy allergy
left join allergy.HealthcareWorker healthcareWorker
left join healthcareWorker.LatticeFacility latticeFacility
Where latticeFacility.Id in (134,342))))
转型后:
select allergy0_.AllergyID as AllergyID41_, allergy0_.RowVersionNumber as RowVersi2_41_, allergy0_.AllergyDate as AllergyD3_41_, allergy0_.Comments as Comments41_, allergy0_.InactivationDate as Inactiva5_41_, allergy0_.CreatedAt as CreatedAt41_, allergy0_.CreatedBy as CreatedBy41_, allergy0_.LastModAt as LastModAt41_, allergy0_.LastModBy as LastModBy41_, allergy0_.HealthcareWorkerID as Healthc10_41_, allergy0_.AllergyTypeID as Allergy11_41_, allergy0_.AllergySubstanceID as Allergy12_41_, allergy0_.MedicationItemID as Medicat13_41_, allergy0_.MedicalPractitionerConfirmedID as Medical14_41_, allergy0_.InactivationReasonID as Inactiv15_41_, case when allergy0_.InactivationDate is not null then case when allergy0_.InactivationReasonID is not null then 0 else 1 end else 1 end as formula4_ from tblAllergy allergy0_ left outer join tblHealthcareWorker healthcare1_ on allergy0_.HealthcareWorkerID=healthcare1_.HealthcareWorkerID where 1=1 and healthcare1_.HealthcareWorkerID=@p0 and (allergy0_.AllergyID in (select allergy2_.AllergyID from tblAllergy allergy2_ where latticefac4_.LatticeFacilityID in (134,342))
这会生成异常无法绑定多部分标识符“latticefac4_.LatticeFacilityID”。显然因为联接已从子查询中删除。
我是NHibernate的新手,所以任何建议都值得赞赏。
由于
答案 0 :(得分:0)
所以这最终成为NHibernate无法识别表别名的问题。事实证明,联接的子查询别名需要与外部查询别名不同。