如何将 HQL 结果转换为 List其中 T 是映射类

发布于 2024-09-10 09:00:05 字数 1411 浏览 2 评论 0原文

我有这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不知在何时 2024-09-17 09:00:05

假设 HistoricEvent 是一个映射实体,以下内容应为您提供所需内容:

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 
                            )" 
               ).AddEntity(typeof(HistoricEvent)); 

            q.SetParameter("lineCompany",lineCompany.Id); 
            q.SetCacheable(false); 

            var results = q.List<HistoricEvent>().ToList(); 

请参阅 相关文档了解更多详细信息。

Assuming HistoricEvent is a mapped entity, the following should give you what you're looking for:

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 
                            )" 
               ).AddEntity(typeof(HistoricEvent)); 

            q.SetParameter("lineCompany",lineCompany.Id); 
            q.SetCacheable(false); 

            var results = q.List<HistoricEvent>().ToList(); 

See the relevant documentation for further details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文