Hql + SetFirstResult + SetMaxResult + Lazy Loading

时间:2011-11-21 18:16:59

标签: nhibernate hql

我有这个查询

  var hql = @"from Table1 tbl1
                            left join fetch tbl1.Table2";

 var c =session.CreateQuery(hql).SetFirstResult(1).SetMaxResults(5)
                            .List<Table1>().ToList();

它继续死亡。

NHibernate.Exceptions.GenericADOException was unhandled by user code
  Message=Could not execute query[SQL: SQL not available]
  Source=NHibernate
  SqlString=SQL not available
  StackTrace:
       at NHibernate.Impl.SessionImpl.List(String query, QueryParameters queryParameters, IList results)
       at NHibernate.Impl.SessionImpl.List[T](String query, QueryParameters parameters)
       at NHibernate.Impl.QueryImpl.List[T]()
       at MvcApplication1.Controllers.Default1Controller.ThenInHql() in MvcApplication1\MvcApplication1\Controllers\Default1Controller.cs:line 152
       at MvcApplication1.Controllers.Default1Controller.Index() in MvcApplication1\MvcApplication1\Controllers\Default1Controller.cs:line 21
       at lambda_method(Closure , ControllerBase , Object[] )
       at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
       at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
       at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12()
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
  InnerException: System.ArgumentNullException
       Message=Value cannot be null.
Parameter name: source
       Source=System.Core
       ParamName=source
       StackTrace:
            at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
            at NHibernate.Engine.QueryParameters.CreateCopyUsing(RowSelection selection)
            at NHibernate.Hql.Ast.ANTLR.QueryTranslatorImpl.List(ISessionImplementor session, QueryParameters queryParameters)
            at NHibernate.Engine.Query.HQLQueryPlan.PerformList(QueryParameters queryParameters, ISessionImplementor session, IList results)
            at NHibernate.Impl.SessionImpl.List(String query, QueryParameters queryParameters, IList results)
       InnerException: 

编辑 nhibernate探查器告诉我这就像是一个投射问题?

ERROR: 
The value "System.Object[]" is not of type "MvcApplication1.Models.Table1" and cannot be used in this generic collection.
Parameter name: value

1 个答案:

答案 0 :(得分:1)

NHibernate ICriteria的SetFirstResult基于ZERO!第一个结果可能表明它以1开头,但它没有。 SetFirstResult(1).SetMaxResults(5)可能不是你想要做的。

将其更改为SetFirstResult(0).SetMaxResults(5)