来自 criteriaQuery 的 nhibernate NamedQuery -->需要帮助

发布于 2024-10-22 20:37:29 字数 3772 浏览 1 评论 0原文

我正在尝试将工作条件查询转换为命名查询,但语法不正确。 hql 版本显然需要指定类型和 id 参数。

查询是通过 ANY 映射的类(也在下面)

有人可以帮助我获得正确的语法吗?

干杯,
Berryl

工作标准查询

    result = _session
        .CreateCriteria<Allocation>()
        .Add(Restrictions.Eq(propName_Resource, resource))
        .Add(Restrictions.Between(propName_StartTime, (DateTime)searchRange.Start, (DateTime)searchRange.End))
        .AddOrder(Order.Asc(propName_StartTime))
        .List<Allocation>();

                produces
SELECT this_.AllocationId as Allocati1_2_0_, this_.ResourceType as Resource2_2_0_, this_.ResourceId as ResourceId2_0_, this_.ActivityType as Activity4_2_0_, 
        this_.ActivityId as ActivityId2_0_, this_.StartTime as StartTime2_0_, this_.EndTime as EndTime2_0_, this_.PostingTime as PostingT8_2_0_ 
FROM Allocations this_ 
WHERE this_.ResourceType = @p0 and this_.ResourceId = @p1 and this_.StartTime between @p2 and @p3 ORDER BY this_.StartTime asc;@p0 = 'EMPLOYEE' [Type: String (0)], @p1 = 98304 [Type: Int32 (0)], @p2 = 3/14/2011 12:00:00 AM [Type: DateTime (0)], @p3 = 3/20/2011 11:59:59 PM [Type: DateTime (0)] 2011-03-14 Mon - 2011-03-20 Sun

任何映射(有效!)

<any name="Resource" cascade="all" id-type="System.Int32" meta-type="System.String"
     >
  <meta-value value="EMPLOYEE" class="Employee, ..." />
  <meta-value value="Facility" class="Facility, ..." />

  <column name="ResourceType"/>
  <column name="ResourceId"/>
</any>

到目前为止命名查询(不起作用)

我认为这是 hql 语法,但错误消息说缺少参数

....
          select a 
          from Allocation a 
          where a.Resource = :resource 
            and a.TimeRange.StartTime between :periodStart and :periodEnd
          order by a.TimeRange.StartTime
....

        return _session.GetNamedQuery("FetchByResourceForDateRange")
            .SetEntity("resource", resource)
            .SetDateTime("periodStart", searchRange.Start)
            .SetDateTime("periodEnd", searchRange.End)
            .List<Allocation>();

NHibernate.Exceptions.GenericADOException : could not execute query
[ select allocation0_.AllocationId as Allocati1_2_, allocation0_.ResourceType as   Resource2_2_, allocation0_.ResourceId as ResourceId2_, allocation0_.ActivityType as Activity4_2_, allocation0_.ActivityId as ActivityId2_, allocation0_.StartTime as StartTime2_, allocation0_.EndTime as EndTime2_, allocation0_.PostingTime as PostingT8_2_ from Allocations allocation0_ where allocation0_.ResourceType=@p0 and allocation0_.ResourceId=@p1 and (allocation0_.StartTime between @p2 and @p3) order by allocation0_.StartTime ]
  Name:resource - Value:George Washington 000001  Name:periodStart - Value:3/14/2011 12:00:00 AM  Name:periodEnd - Value:3/20/2011 11:59:59 PM
[SQL: select ... 
    from Allocations allocation0_ 
where allocation0_.ResourceType=@p0 and allocation0_.ResourceId=@p1 and (allocation0_.StartTime between @p2 and @p3) 
order by allocation0_.StartTime]

 ----> System.Data.SQLite.SQLiteException : SQLite error
Insufficient parameters supplied to the command

更新

此组合执行但给出错误回答。尚无法判断它离我需要的更近还是更远

          select a 
          from Allocation a 
          where a.Resource.class = :class and a.Resource.id = :id 
            and a.TimeRange.StartTime between :periodStart and :periodEnd
          order by a.TimeRange.StartTime

        return _session.GetNamedQuery("FetchByResourceForDateRange")
            .SetString("class", typeof(Resource).FullName)
            .SetInt32("id", resource.Id)
            .SetDateTime("periodStart", searchRange.Start)
            .SetDateTime("periodEnd", searchRange.End)
            .List<Allocation>();

I am trying to convert a working criteria query into a named query, and not getting the syntax right. The hql version apparently wants both the type and id params specified.

The query is over a class mapped with ANY (below also)

Can someone give me a hand getting the syntax right?

Cheers,
Berryl

WORKING criteria query

    result = _session
        .CreateCriteria<Allocation>()
        .Add(Restrictions.Eq(propName_Resource, resource))
        .Add(Restrictions.Between(propName_StartTime, (DateTime)searchRange.Start, (DateTime)searchRange.End))
        .AddOrder(Order.Asc(propName_StartTime))
        .List<Allocation>();

                produces
SELECT this_.AllocationId as Allocati1_2_0_, this_.ResourceType as Resource2_2_0_, this_.ResourceId as ResourceId2_0_, this_.ActivityType as Activity4_2_0_, 
        this_.ActivityId as ActivityId2_0_, this_.StartTime as StartTime2_0_, this_.EndTime as EndTime2_0_, this_.PostingTime as PostingT8_2_0_ 
FROM Allocations this_ 
WHERE this_.ResourceType = @p0 and this_.ResourceId = @p1 and this_.StartTime between @p2 and @p3 ORDER BY this_.StartTime asc;@p0 = 'EMPLOYEE' [Type: String (0)], @p1 = 98304 [Type: Int32 (0)], @p2 = 3/14/2011 12:00:00 AM [Type: DateTime (0)], @p3 = 3/20/2011 11:59:59 PM [Type: DateTime (0)] 2011-03-14 Mon - 2011-03-20 Sun

The ANY Mapping (which WORKS!)

<any name="Resource" cascade="all" id-type="System.Int32" meta-type="System.String"
     >
  <meta-value value="EMPLOYEE" class="Employee, ..." />
  <meta-value value="Facility" class="Facility, ..." />

  <column name="ResourceType"/>
  <column name="ResourceId"/>
</any>

NAMED QUERY SO FAR (NOT working)

I think this is the hql syntax but error message says a param is missing

....
          select a 
          from Allocation a 
          where a.Resource = :resource 
            and a.TimeRange.StartTime between :periodStart and :periodEnd
          order by a.TimeRange.StartTime
....

        return _session.GetNamedQuery("FetchByResourceForDateRange")
            .SetEntity("resource", resource)
            .SetDateTime("periodStart", searchRange.Start)
            .SetDateTime("periodEnd", searchRange.End)
            .List<Allocation>();

NHibernate.Exceptions.GenericADOException : could not execute query
[ select allocation0_.AllocationId as Allocati1_2_, allocation0_.ResourceType as   Resource2_2_, allocation0_.ResourceId as ResourceId2_, allocation0_.ActivityType as Activity4_2_, allocation0_.ActivityId as ActivityId2_, allocation0_.StartTime as StartTime2_, allocation0_.EndTime as EndTime2_, allocation0_.PostingTime as PostingT8_2_ from Allocations allocation0_ where allocation0_.ResourceType=@p0 and allocation0_.ResourceId=@p1 and (allocation0_.StartTime between @p2 and @p3) order by allocation0_.StartTime ]
  Name:resource - Value:George Washington 000001  Name:periodStart - Value:3/14/2011 12:00:00 AM  Name:periodEnd - Value:3/20/2011 11:59:59 PM
[SQL: select ... 
    from Allocations allocation0_ 
where allocation0_.ResourceType=@p0 and allocation0_.ResourceId=@p1 and (allocation0_.StartTime between @p2 and @p3) 
order by allocation0_.StartTime]

 ----> System.Data.SQLite.SQLiteException : SQLite error
Insufficient parameters supplied to the command

UPDATE

this combination executes but gives the wrong answer. can't tell if it is closer or further from what I need yet

          select a 
          from Allocation a 
          where a.Resource.class = :class and a.Resource.id = :id 
            and a.TimeRange.StartTime between :periodStart and :periodEnd
          order by a.TimeRange.StartTime

        return _session.GetNamedQuery("FetchByResourceForDateRange")
            .SetString("class", typeof(Resource).FullName)
            .SetInt32("id", resource.Id)
            .SetDateTime("periodStart", searchRange.Start)
            .SetDateTime("periodEnd", searchRange.End)
            .List<Allocation>();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

不知所踪 2024-10-29 20:37:29

好的,根据 NHib 首席开发人员 Fabio Maulo 的说法:

“你必须使用你的元数据值,而不是 typeof(entity).FullName”

因此,在我的例子中,以下内容使我工作的最后一个 hql 变得更丑:

return _session.GetNamedQuery("FetchByResourceForDateRange")
        .SetString("class", "EMPLOYEE")
        .SetInt32("id", resource.Id)
        .SetDateTime("periodStart", searchRange.Start)
        .SetDateTime("periodEnd", searchRange.End)
        .List<Allocation>();

比我想象的要丑一些有希望,但我会接受:--)

HTH,
贝里尔

Ok, per Fabio Maulo, lead dev on NHib:

"You have to use your meta-data values not the typeof(entity).FullName"

So in my case the following made the last hql I had work:

return _session.GetNamedQuery("FetchByResourceForDateRange")
        .SetString("class", "EMPLOYEE")
        .SetInt32("id", resource.Id)
        .SetDateTime("periodStart", searchRange.Start)
        .SetDateTime("periodEnd", searchRange.End)
        .List<Allocation>();

A bit uglier than I would have hoped but I'll take it :--)

HTH,
Berryl

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