Requesting a byte property with nhibernate causes an invalid error on startup

Is there a problem with nhibernate 3.1.0.4000 where the byte property is being requested:

byte code = 2; Group g = Repository<Group>.FindOne(p => p.Code == code); 

Exceptional text:

 Cause 'Specified cast is not valid.' [InvalidCastException: Specified cast is not valid.] NHibernate.Type.ByteType.Set(IDbCommand cmd, Object value, Int32 index) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Type\ByteType.cs:44 NHibernate.Type.NullableType.NullSafeSet(IDbCommand cmd, Object value, Int32 index) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Type\NullableType.cs:180 ... [GenericADOException: could not execute query [ select group0_.LabourLawGroupId as LabourLa1_235_, group0_.Code as Code235_ from personnel.LabourLawGroup group0_ where group0_.Code=? ] Name:p1 - Value:4 [SQL: select group0_.LabourLawGroupId as LabourLa1_235_, group0_.Code as Code235_ from personnel.LabourLawGroup group0_ where group0_.Code=?]] NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Loader\Loader.cs:1703 NHibernate.Loader.Loader.ListIgnoreQueryCache(ISessionImplementor session, QueryParameters queryParameters) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Loader\Loader.cs:1601 NHibernate.Loader.Loader.List(ISessionImplementor session, QueryParameters queryParameters, ISet`1 querySpaces, IType[] resultTypes) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Loader\Loader.cs:1591 NHibernate.Hql.Ast.ANTLR.Loader.QueryLoader.List(ISessionImplementor session, QueryParameters queryParameters) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Hql\Ast\ANTLR\Loader\QueryLoader.cs:300 NHibernate.Hql.Ast.ANTLR.QueryTranslatorImpl.List(ISessionImplementor session, QueryParameters queryParameters) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Hql\Ast\ANTLR\QueryTranslatorImpl.cs:108 NHibernate.Engine.Query.HQLQueryPlan.PerformList(QueryParameters queryParameters, ISessionImplementor session, IList results) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Engine\Query\HQLQueryPlan.cs:78 NHibernate.Impl.SessionImpl.List(IQueryExpression queryExpression, QueryParameters queryParameters, IList results) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Impl\SessionImpl.cs:645 NHibernate.Impl.AbstractSessionImpl.List(IQueryExpression queryExpression, QueryParameters parameters) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Impl\AbstractSessionImpl.cs:92 NHibernate.Impl.ExpressionQueryImpl.List() in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Impl\ExpressionQueryImpl.cs:60 NHibernate.Linq.NhQueryProvider.ExecuteQuery(NhLinqExpression nhLinqExpression, IQuery query, NhLinqExpression nhQuery) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Linq\NhQueryProvider.cs:79 NHibernate.Linq.NhQueryProvider.Execute(Expression expression) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Linq\NhQueryProvider.cs:103 System.Linq.Queryable.SingleOrDefault(IQueryable`1 source) +265 Azarakhsh.Framework.Repository.NHibernateRepository`1.FindOne(Expression`1 expression) +223 Azarakhsh.Framework.Repository.Repository`1.FindOne(Expression`1 expression) +100 
+6
source share
3 answers

I get the same error with the same version of NHibernate using the Linq2Nhibernate query, for example:

 var details = (from d in repository.AllEntities where (d.OtherTable.Field == someCriteria && d.LineIndex == 1) select d).ToList(); 

In this case, LineIndex displayed as a .NET Byte (cannot be NULL). The top of my stack trace is as follows:

 NHibernate.Type.ByteType.Set(IDbCommand cmd, Object value, Int32 index) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Type\ByteType.cs: line 44 NHibernate.Type.NullableType.NullSafeSet(IDbCommand cmd, Object value, Int32 index) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Type\NullableType.cs: line 180 NHibernate.Type.NullableType.NullSafeSet(IDbCommand st, Object value, Int32 index, ISessionImplementor session) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Type\NullableType.cs: line 139 NHibernate.Engine.QueryParameters.BindParameters(IDbCommand command, Int32 start, ISessionImplementor session) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Engine\QueryParameters.cs: line 630 ... 

Looking at the source, the Set method looks like this:

 public override void Set(IDbCommand cmd, object value, int index) { ((IDataParameter) cmd.Parameters[index]).Value = (byte) value; } 

Therefore, it tries to pass the object to Byte and get an invalid listing. Interestingly, when I passed my literal 1 to Byte , I still get the same error. This also happens if I use const byte . So I think this is something higher than the stack that takes Byte and does something weird with it.

It is particularly interesting that it makes a NullSafeSet when the field is not NULL. As an experiment, I changed my LineIndex property to int , and I did not get an invalid exception. This seems like a bug in NHibernate. I will try to stick to this in the NHibernate tracker block.

Edit

Please note that this similar error NH-2485 was closed as not a problem.

Another similar / overlapping error:

  • NH-2789 LINQ query by byte? simple property does not work on MSSQL 2005 (tinyint)

I posted this error:

  • NH-2812 Running a Linq query for a non-null property raises an InvalidCastException

Edit

Version 3.3.1 and higher solves this problem.

+6
source

Just use short instead of byte

+2
source

I had this exact problem today with NHibernate 3.2.x. The errors listed by Scott Whitlock have been flagged as fixed in 3.3.x , and I can confirm that the NHibernate update resolved this issue.

+1
source

Source: https://habr.com/ru/post/891638/


All Articles