简体   繁体   English

nHibernate SetFirstResult & SetMaxResults 问题

[英]nHibernate SetFirstResult & SetMaxResults Problems

I am using nHibernate and trying to implement some paging.我正在使用 nHibernate 并尝试实现一些分页。 If I run this code如果我运行此代码

IList list = session.CreateQuery("FROM Author").List();

it returns 8 records.它返回 8 条记录。 If I run this code though如果我运行这段代码

IList list = session.CreateQuery("FROM Author")
    .SetFirstResult(1).SetMaxResults(5).List();

it returns 0 records.它返回 0 条记录。 When I look at the SQL generated I can't see that there is any paging logic.当我查看生成的 SQL 时,我看不到有任何分页逻辑。

What is the most likely errors with this?最可能的错误是什么?

I'm not sure about NHibernate, but in Java the result index is 0-based.我不确定 NHibernate,但在 Java 中,结果索引是从 0 开始的。 Try calling.SetFirstResult(0) instead of.SetFirstResult(1), otherwise if there is only one row, it will return 0 results.尝试调用.SetFirstResult(0)而不是.SetFirstResult(1),否则如果只有一行,会返回0个结果。 It sounds like you have 8 rows, though, so I don't know why you wouldn't get some results in any case.不过,听起来你有 8 行,所以我不知道为什么无论如何你都不会得到一些结果。

You have the syntax correct, so as long as there is data in that table, I'm not sure why it would return 0 results.您的语法正确,因此只要该表中有数据,我不确定它为什么会返回 0 结果。

As far as the generated SQL, MSSQL Server does not support the LIMIT and OFFSET commands so paging can't be implemented in that fashion on the server.就生成的 SQL 而言,MSSQL Server 不支持 LIMIT 和 OFFSET 命令,因此无法在服务器上以这种方式实现分页。 (nb My understanding is that SQL Server 2005 has improved methods for paging, but still doesn't support LIMIT or OFFSET) So, with SQL 2000, all rows must be retrieved and then the subset selected from that result set. (nb 我的理解是 SQL Server 2005 改进了分页方法,但仍然不支持 LIMIT 或 OFFSET)因此,对于 SQL 2000,必须检索所有行,然后从该结果集中选择子集。

The only other thing I can suggest is check that your query is within a separate transaction.我唯一可以建议的另一件事是检查您的查询是否在单独的事务中。 I have seen posts that suggest this might help.我已经看到建议这可能会有所帮助的帖子。 There may be something to it because my unit tests with SetFirstResult/SetMaxResults are successful.可能有一些问题,因为我使用 SetFirstResult/SetMaxResults 进行的单元测试是成功的。

Maybe check your Dialect and Connection Provider in web.config or app.config也许在 web.config 或 app.config 中检查您的方言和连接提供程序

  <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
  <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
  <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM