Invalid cast exception while querying 'time' field with NHibernate
I have a complex query executed with Nhibernate using session.CreateSQLQuery. And during execution I got the next exception:
Unable to cast object of type 'System.TimeSpan' to type 'System.IConvertible'.
It seems that NHibernate can't correctly handle field type 'time' and expects integer value there. It works fine with regular HQL query over entity mapped using xml mapping file. However in my case I need to run an sql query, so it's not possible to specify mapping. Any ideas how to fix it? Looks like an NHibernate bug.
NHibernate verison: 3.3.1.4
Here is the full stack trace:
Unable to cast object of type 'System.TimeSpan' to type 'System.IConvertible'. at System.Convert.ToInt64(Object value)
at NHibernate.Type.TimeSpanType.Get(IDataReader rs, Int32 index) in p:nhibernate-coresrcNHibernateTypeTimeSpanType.cs:line 32 Inner Exception: Input string '08:00:00' was not in the correct format.
at NHibernate.Type.TimeSpanType.Get(IDataReader rs, Int32 index) in p:nhibernate-coresrcNHibernateTypeTimeSpanType.cs:line 36 at NHibernate.Type.NullableType.NullSafeGet(IDataReader rs, String name) in p:nhibernate-coresrcNHibernateTypeNullableType.cs:line 253
at NHibernate.Loader.Custom.CustomLoader.ScalarResultColumnProcessor.Extract(Object[] data, IDataReader resultSet, ISessionImplementor session) in p:nhibernate-coresrcNHibernateLoaderCustomCustomLoader.cs:line 458 at NHibernate.Loader.Custom.CustomLoader.ResultRowProcessor.BuildResultRow(Object[] data, IDataReader resultSet, Boolean hasTransformer, ISessionImplementor session) in p:nhibernate-coresrcNHibernateLoaderCustomCustomLoader.cs:line 395 at NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) in p:nhibernate-coresrcNHibernateLoaderLoader.cs:line 473 at NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) in p:nhibernate-coresrcNHibernateLoaderLoader.cs:line 251 at NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters) in p:nhibernate-coresrcNHibernateLoade rLoader.cs:line 1564 could not execute query [query] at NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters) in p:nhibernate-coresrcNHibernateLoaderLoader.cs:line 1573 at NHibernate.Loader.Loader.ListIgnoreQueryCache(ISessionImplementor session, QueryParameters queryParameters) in p:nhibernate-coresrcNHibernateLoaderLoader.cs:line 1472 at NHibernate.Impl.SessionImpl.ListCustomQuery(ICustomQuery customQuery, QueryParameters queryParameters, IList results) in p:nhibernate-coresrcNHibernateImplSessionImpl.cs:line 2104 at NHibernate.Impl.SessionImpl.List(NativeSQLQuerySpecification spec, QueryParameters queryParameters, IList results) in p:nhibernate-coresrcNHibernateImplSessionImpl.cs:line 2091 at NHibernate.Impl.SessionImpl.List[T](NativeSQLQuerySpecification spec, QueryParameters queryParameters) in p:nhibernate-coresrcNHibernateImplSessionImpl.cs:line 2078 at NHibernate.Impl.SqlQueryImpl.ListT in p:nhibernate-coresrc NHibernateImplSqlQueryImpl.cs:line 160
UPDATE: Here is simple code that helps to reproduce the problem. TimeValue has 'time(0) null' type in sql server.
ISession session = TransactionScopeManager.Instance.GetSession();
iQuery = session.CreateSQLQuery("select TimeValue from TestTable");
result = iQuery.List();
链接地址: http://www.djcxy.com/p/71064.html