Castle ActiveRecord HQL查询抛出异常:“无法将类型为'System.Guid'的对象强制转换为'System.IConvertible'”

时间:2012-02-24 03:27:11

标签: .net sql-server nhibernate hql castle-activerecord

我最近遇到了一个让我感到难过的问题,主要是因为它出现的频率不稳定。

我正在执行以下HQL查询:

SELECT prod.ProductType.ID,
    prod.Provider.ProviderType.ID,
    r.Term.ID,
    MAX(r.InterestRate),
    MAX(CAST(r.ID AS String))
FROM Product prod
    JOIN prod.Rates r
WHERE prod.ArchivedAt IS NULL
    AND prod.Active = TRUE
    AND prod.Provider.ArchivedAt IS NULL
    AND prod.Provider.Enabled = TRUE
GROUP BY prod.ProductType.ID,
    r.Term.ID,
    prod.Provider.ProviderType.ID

重要的是要注意所有ID字段都是GUID,并且选择的最后一个值是我从集合中返回任意Guid的方式。我得到的那一批并不重要。

执行此查询并不总是导致失败。事实上,在整个调试周期中,一切都很好,查询正确执行并返回正确的数据。

然而,一旦代码被推向生产,虽然初始测试看起来很好,一些用户报告错误,因此查询了错误日志,并且出现了多次出现的错误:< / p>

Castle.ActiveRecord.Framework.ActiveRecordException: Could not perform ExecuteQuery for Product ---> NHibernate.ADOException: could not execute query
[ select product0_.ProductTypeID as col_0_0_, provider2_.ProviderTypeID as col_1_0_, rates1_.TermID as col_2_0_, MAX(rates1_.InterestRate) as col_3_0_, cast(MAX(cast(rates1_.ID as NVARCHAR(255))) as UNIQUEIDENTIFIER) as col_4_0_ from Product product0_ inner join ProductRate rates1_ on product0_.ID=rates1_.ProductID, Provider provider2_ where product0_.ProviderID=provider2_.ID and (product0_.ArchivedAt is null) and product0_.Active=1 and (provider2_.ArchivedAt is null) and provider2_.Enabled=1 group by product0_.ProductTypeID , rates1_.TermID , provider2_.ProviderTypeID ]
---> System.FormatException: Input string 'f94be876-d5b6-4e35-bf2c-a0010099cd9a' was not in the correct format. ---> System.InvalidCastException: Unable to cast object of type 'System.Guid' to type 'System.IConvertible'.
    at System.Convert.ToInt32(Object value)
    at NHibernate.Type.Int32Type.Get(IDataReader rs, Int32 index)
    --- End of inner exception stack trace ---
    at NHibernate.Type.Int32Type.Get(IDataReader rs, Int32 index)
    at NHibernate.Type.NullableType.NullSafeGet(IDataReader rs, String name)
    at NHibernate.Type.NullableType.NullSafeGet(IDataReader rs, String[] names, ISessionImplementor session, Object owner)
    at NHibernate.Dialect.Function.CastFunction.LazyType.NullSafeGet(IDataReader rs, String[] names, ISessionImplementor session, Object owner)
    at NHibernate.Hql.Ast.ANTLR.Loader.QueryLoader.GetResultColumnOrRow(Object[] row, IResultTransformer resultTransformer, IDataReader rs, ISessionImplementor session)
    at NHibernate.Loader.Loader.GetRowFromResultSet(IDataReader resultSet, ISessionImplementor session, QueryParameters queryParameters, LockMode[] lockModeArray, EntityKey optionalObjectKey, IList hydratedObjects, EntityKey[] keys, Boolean returnProxies)
    at NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies)
    at NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies)
    at NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters)
    --- End of inner exception stack trace ---
    at NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters)
    at NHibernate.Loader.Loader.ListIgnoreQueryCache(ISessionImplementor session, QueryParameters queryParameters)
    at NHibernate.Loader.Loader.List(ISessionImplementor session, QueryParameters queryParameters, ISet`1 querySpaces, IType[] resultTypes)
    at NHibernate.Hql.Ast.ANTLR.Loader.QueryLoader.List(ISessionImplementor session, QueryParameters queryParameters)
    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)
    at NHibernate.Impl.SessionImpl.List(String query, QueryParameters parameters)
    at NHibernate.Impl.QueryImpl.List()
    at Castle.ActiveRecord.ActiveRecordBaseQuery.InternalExecute(ISession session)
    at Castle.ActiveRecord.ActiveRecordBaseQuery.Castle.ActiveRecord.IActiveRecordQuery.Execute(ISession session)
    at Castle.ActiveRecord.ActiveRecordBase.ExecuteQuery(IActiveRecordQuery query)
    --- End of inner exception stack trace ---
    at Castle.ActiveRecord.ActiveRecordBase.ExecuteQuery(IActiveRecordQuery query)
    at Castle.ActiveRecord.ActiveRecordMediator.ExecuteQuery(IActiveRecordQuery q)

现在这首先让我感到困惑,因为虽然我可以很容易地看到这个guid如何无法解析为int,但我不明白为什么首先要进行这样的尝试,其次,因为上面的SQL查询运行没有问题。

任何人都可以解释此事吗?即使是最小的信息也会有所帮助,因为目前我甚至不知道从哪里开始寻找故障。

编辑:扩展堆栈跟踪

0 个答案:

没有答案