nhibernate / nhibernate-core

NHibernate Object Relational Mapper
https://nhibernate.info
GNU Lesser General Public License v2.1
2.13k stars 928 forks source link

HQL fails with Second Level Cache enabled NHibernate.PropertyAccessException: could not get a field value by reflection getter of Entity.AcademicPeriod.m_ID #3431

Open EngSayed opened 1 year ago

EngSayed commented 1 year ago

We have a search screen which started to fail once enabling second level cache and enabling cache for query. Here is the generated HQL: select count(distinct a) from Entity.CourseOffering a where (a.AcademicPeriod = :h0 AND a.CourseID Like :h1)

If I change it to the following at runtime then it works fine select count(distinct a) from Entity.CourseOffering a where (a.AcademicPeriod.ID = :h0 AND a.CourseID Like :h1)

NHibernate.PropertyAccessException: could not get a field value by reflection getter of TRS.CAMS.Academic.Entity.AcademicPeriod.m_ID ---> System.ArgumentException: Field 'm_ID' defined on type 'TRS.Flint.BLL.Entities.BusinessObject`1[Entity.AcademicPeriod]' is not a field on the target object which is of type 'System.Int64'.
   at System.Reflection.RtFieldInfo.CheckConsistency(Object target)
   at System.Reflection.RtFieldInfo.InternalGetValue(Object obj, StackCrawlMark& stackMark)
   at System.Reflection.RtFieldInfo.GetValue(Object obj)
   at NHibernate.Properties.FieldAccessor.FieldGetter.Get(Object target)
   --- End of inner exception stack trace ---
   at NHibernate.Properties.FieldAccessor.FieldGetter.Get(Object target)
   at NHibernate.Tuple.Entity.PocoEntityTuplizer.GetIdentifierPropertyValue(Object entity)
   at NHibernate.Tuple.Entity.AbstractEntityTuplizer.GetIdentifier(Object entity)
   at NHibernate.Type.EntityType.ToLoggableString(Object value, ISessionFactoryImplementor factory)
   at NHibernate.Impl.Printer.<ToString>b__5_0(KeyValuePair`2 ntv)
   at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
   at NHibernate.Util.CollectionPrinter.ToString(IEnumerable elements)
   at NHibernate.Cache.QueryKey.ToString()
   at NHibernate.Caches.StackExchangeRedis.DefaultRegionStrategy.GetCacheKey(Object value)
   at NHibernate.Caches.StackExchangeRedis.AbstractRegionStrategy.Get(Object key)
   at NHibernate.Caches.StackExchangeRedis.DefaultRegionStrategy.Get(Object key, Int32 retries)
   at NHibernate.Cache.StandardQueryCache.Get(QueryKey key, QueryParameters queryParameters, ICacheAssembler[] returnTypes, ISet`1 spaces, ISessionImplementor session)
   at NHibernate.Cache.QueryCacheExtensions.Get(IQueryCache queryCache, QueryKey key, QueryParameters queryParameters, ICacheAssembler[] returnTypes, ISet`1 spaces, ISessionImplementor session)
   at NHibernate.Loader.Loader.GetResultFromQueryCache(ISessionImplementor session, QueryParameters queryParameters, ISet`1 querySpaces, IQueryCache queryCache, QueryKey key)
   at NHibernate.Loader.Loader.ListUsingQueryCache(ISessionImplementor session, QueryParameters queryParameters, ISet`1 querySpaces)
   at NHibernate.Loader.Hql.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(IQueryExpression queryExpression, QueryParameters queryParameters, IList results, Object filterConnection)
   at NHibernate.Impl.SessionImpl.List(IQueryExpression queryExpression, QueryParameters queryParameters, IList results)
   at NHibernate.Impl.AbstractSessionImpl.List(IQueryExpression queryExpression, QueryParameters parameters)
   at NHibernate.Impl.AbstractQueryImpl2.List()
   at NHibernate.Impl.AbstractQueryImpl.UniqueResult[T]()
   at Entities.SmartQuery`1.SearchCore(String criteria, Boolean& getCnt, Int32 pageSize, Boolean populateMatchList, SmartQueryFilterCriteria sq, String currentSortField, Int32 pageNumber, Int64& rowCount, String& currentSortDirection, ITransactionContext context) in ~\Entity\DB\SmartQueryT.cs:line 1375
EngSayed commented 1 year ago

Also if I remove query.SetCacheable(true).SetCacheRegion(cacheName) then it also works fine so the issue happens when enabling query cache.