2011-11-21 40 views
0

我有這個疑問HQL + SetFirstResult + SetMaxResult +延遲加載

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

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

它不斷dieing。

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

的NHibernate的ICriteria的SetFirstResult是從零開始!第一個結果可能表明它從1開始,但不是。 SetFirstResult(1).SetMaxResults(5)可能不是你想要做的。

將其更改爲SetFirstResult(0).SetMaxResults(5)

+0

什麼是o/p?我不明白如果我擺脫SetFirstResult()和SetMaxResults()並執行相同的查詢。它將運行並堅持table1中的所有內容,而table1中將包含Table2的集合。那麼爲什麼它可以在這種情況下算出來呢?所以,如果我做選項2,我必須指定我需要從table1和2的每一列? – chobo2

+0

srry我認爲誤解了你的問題..更新了答案,看看是否有助於 – Baz1nga

+0

的東西來思考:http://stackoverflow.com/questions/2461658/nhibernate-2nd-lvl-cache-custom-query-sqldialect – Baz1nga