将HQL转换为另一个nHibernate查询表单
本文关键字:查询 查询表 表单 nHibernate 另一个 HQL 转换 | 更新日期: 2023-09-27 18:22:05
HQL查询为:
from BusinessObject as businessobject
left outer join businessobject.BussinesPartner as businesspartner
left outer join businessobject.Document as document
left outer join businessobject.Group as group ";
where businessobject.Date >= :datefrom and businessobject.Date <= :dateto
and (:filter = ''
or :filter is null
or businesspartner.Name like '%' || :filter || '%'
or group.Name like '%' || :filter || '%'
or document.Name like '%' || :filter || '%')
我不想使用HQL。我还想要一个强类型的结果集合,而不使用不方便和混乱的SetResultTransformer。我更喜欢使用查询<>(),但我不知道如何强制外部联接。
当我在Query<>中写入此内容时()表单中,它不会返回日期在正确范围内的结果,并且如果(例如)BusinessPartner为null,则filter="。虽然这些对象的"where"逻辑表达式为true,但由于使用了内部联接,它们看起来无论如何都不在结果集中:
session.Query<BusinessObject>()
.Where(x =>
(x.Date >= criteria.DateFrom && x.Date <= criteria.DateTo
&& (criteria.Filter == ""
|| x.BusinessPartner.Name.Contains(criteria.Filter)
|| x.Group.Name.Contains(criteria.Filter)
|| x.Document.Name.Contains(criteria.Filter))))
这不起作用,因为引用BusinessPartner、Group或Document的字段中有null的任何行都不会返回(表示内部联接)。
是否可以使用Query<>执行我想要的操作()?还是有标准?
我不知道Query<>(),但使用ICriteria可以控制JOIN的类型,并使用CreateAlias获得强类型结果。
ICriterion conjunction = Restrictions.Conjunction();
conjunction.Add(Restrictions.Contains(criteria.Filter),"BusinessPartner.Name");
conjunction.Add(Restrictions.Contains(criteria.Filter),"Group.Name");
conjunction.Add(Restrictions.Contains(criteria.Filter),"Document.Name");
ICriteria query = ICriteria.CreateCriteria
.CreateAlias("BusinessPartner", "BusinessPartner", JoinType.LeftOuterJoin)
.CreateAlias("Group", "Group", JoinType.LeftOuterJoin)
.CreateAlias("Document", "Document", JoinType.LeftOuterJoin)
.Add(Restrictions.Between(criteria.DateFrom,criteria.DateTo),"Date")
.Add(conjunction));
List<BusinessObject> results = query.List<BusinessObject>();