如何将 HQL 结果转换为 List其中 T 是映射类
我有这个 nhibernate 查询:
var q =
NHibernateSession.Current.CreateSQLQuery
(
@"SELECT LastestEvents.*
FROM (
SELECT DISTINCT SbQcontainer.Container
FROM HistoricEvents
SbQcontainer WHERE SbQcontainer.LineCompany_Id = :lineCompany) as Sbq
JOIN HistoricEvents as LastestEvents
ON LastestEvents.id = (
SELECT TOP(1) id
FROM HistoricEvents mi
WHERE mi.Container = Sbq.Container and mi.LineCompany_Id = :lineCompany
ORDER BY mi.Date DESC
)"
).SetResultTransformer(Transformers.AliasToBean(typeof(HistoricEvent)));
q.SetParameter("lineCompany",lineCompany.Id);
q.SetCacheable(false);
var results = q.List<HistoricEvent>().ToList();
它查找给定 lineCompany 的每个容器上的最新事件,它有效,但我不知道如何将此结果集设置为 T HistoricEvent 列表,我尝试这一行:
.SetResultTransformer(Transformers.AliasToBean(typeof(HistoricEvent)));
但抛出 NHibernate.PropertyNotFoundException:无法在类“HistoricEvent”中找到属性“Event_Id”的设置器。
有什么方法可以做到这一点吗?或者可以使用 ICriteria API 执行相同的查询?
提前谢谢。
I have this nhibernate query:
var q =
NHibernateSession.Current.CreateSQLQuery
(
@"SELECT LastestEvents.*
FROM (
SELECT DISTINCT SbQcontainer.Container
FROM HistoricEvents
SbQcontainer WHERE SbQcontainer.LineCompany_Id = :lineCompany) as Sbq
JOIN HistoricEvents as LastestEvents
ON LastestEvents.id = (
SELECT TOP(1) id
FROM HistoricEvents mi
WHERE mi.Container = Sbq.Container and mi.LineCompany_Id = :lineCompany
ORDER BY mi.Date DESC
)"
).SetResultTransformer(Transformers.AliasToBean(typeof(HistoricEvent)));
q.SetParameter("lineCompany",lineCompany.Id);
q.SetCacheable(false);
var results = q.List<HistoricEvent>().ToList();
It looks for the lastest events on each container for the given lineCompany, it works, but i dont know how to set this resultset to a list of T HistoricEvent, i try this line:
.SetResultTransformer(Transformers.AliasToBean(typeof(HistoricEvent)));
But throws NHibernate.PropertyNotFoundException:Could not find a setter for property 'Event_Id' in class 'HistoricEvent'.
Is there any way to do this?, or maybe doing this same query using the ICriteria API?
Thx in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设 HistoricEvent 是一个映射实体,以下内容应为您提供所需内容:
请参阅 相关文档了解更多详细信息。
Assuming HistoricEvent is a mapped entity, the following should give you what you're looking for:
See the relevant documentation for further details.