【问题标题】:Nhibernate setFirstResult休眠 setFirstResult
【发布时间】:2013-09-16 15:37:40
【问题描述】:

我在尝试使用 setFirstResult 时遇到了 Nhibernate 版本 3.3.3 的问题。

IQuery q = session.CreateQuery("Select a from SelectionAssignment a ") 
                .SetFirstResult(1)
                .SetMaxResults(10);

var assignments = q.List<SelectionAssignment>();

上面吐出的SQL是这样的:

SELECT TOP (10) asId30_
, asHostId30_
, asDescript3_30_
, asIsChase30_
, asPosition30_
, asGoalTime30_
, asRoute30_
, asActiveta8_30_
, asPassAssi9_30_
, asSummary10_30_
, asOverrid11_30_
, asDeliver12_30_
, asDirectLoad30_
, asAllowDe14_30_
, asVehicle15_30_
, asCustomerId30_
, asTotalWe17_30_
, asTotalItems30_
, asSingleSKU30_
, asSingleB20_30_
, asCreated21_30_
, asStartDate30_
, asEndDate30_
, asPriority30_
, asIsDeleted30_
, asDeleted26_30_
, asDeleted27_30_ FROM 
(
select selectiona0_.Id as Id30_
, selectiona0_.HostId as HostId30_
, selectiona0_.Description as Descript3_30_
, selectiona0_.IsChase as IsChase30_
, selectiona0_.Position as Position30_
, selectiona0_.GoalTime as GoalTime30_
, selectiona0_.Route as Route30_
, selectiona0_.ActivetargetContainer as Activeta8_30_
, selectiona0_.PassAssignment as PassAssi9_30_
, selectiona0_.SummaryPromptType as Summary10_30_
, selectiona0_.OverridePrompt as Overrid11_30_
, selectiona0_.DeliveryLocationId as Deliver12_30_
, selectiona0_.DirectLoad as DirectLoad30_
, selectiona0_.AllowDeliverLocationOverride as AllowDe14_30_
, selectiona0_.VehicleLicense as Vehicle15_30_
, selectiona0_.CustomerId as CustomerId30_
, selectiona0_.TotalWeight as TotalWe17_30_
, selectiona0_.TotalItems as TotalItems30_
, selectiona0_.SingleSKU as SingleSKU30_
, selectiona0_.SingleBatch as SingleB20_30_
, selectiona0_.CreatedDate as Created21_30_
, selectiona0_.StartDate as StartDate30_
, selectiona0_.EndDate as EndDate30_
, selectiona0_.Priority as Priority30_
, selectiona0_.IsDeleted as IsDeleted30_
, selectiona0_.DeletedDate as Deleted26_30_
, selectiona0_.DeletedUser as Deleted27_30_
, ROW_NUMBER() OVER(ORDER BY CURRENT_TIMESTAMP) as __hibernate_sort_row 
from SelectionAssignment selectiona0_ where ( selectiona0_.IsDeleted=0)
) as query
 WHERE query.__hibernate_sort_row > 0 
 ORDER BY query.__hibernate_sort_row

而如果我将参数设置为 .SetFirstResult(0) 它工作正常。

谁能告诉我为什么?或者我该如何解决这个问题?

编辑:抱歉,我收到 Invalid column name 'asId30' 'asHostId30' 等错误消息。列名是 Id、HostId 等。

【问题讨论】:

  • 我想这可能是一个愚蠢的评论,但你能指出问题出在哪里吗?
  • 抱歉编辑解释
  • 可能是方言问题,但是,首先要添加的是您的 select 语句中的 ORDER BY。没有它,First 和 Max 毫无意义。
  • 我按顺序添加了,没有任何区别。我用 2008 年和 2012 年的方言都试过了。它们都产生了错误的查询。
  • 如果使用以下命令切换查询模型会发生什么:var assignments = session.Query().OrderBy(a => a.Id).Skip(1).Take(10)。 ToList();

标签: asp.net-mvc nhibernate


【解决方案1】:

我发现了问题。似乎是列名“SelectionAssignment”导致了一个问题,因为 NHibernate 在生成的 SQL 上使用它自己的别名,该别名需要 10 个字符并添加一个数字。但是,我的列名中的第 10 个和第 11 个字符是“AS”。当它试图将自己的 AS 放在那里时,这似乎会导致与查询发生冲突。

一旦我将列名更改为不同的“PickingAssignment”,问题就得到了解决。

我希望我已经解释得足够清楚,并且它可以帮助其他人解决同样的问题。感谢 jbl 的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-16
    • 1970-01-01
    • 2021-10-21
    • 2015-02-08
    • 2019-04-18
    • 2017-02-06
    相关资源
    最近更新 更多