views:

24

answers:

1

I have following query in NHibernate where the result is a list of DTO's, not entities:

var result = query
                //.SetCacheable(true)
                .SetResultTransformer(new MyDTOTransformer())
                .List<DTO>();

This works with SetCacheable in comment but it throws an IndexOutOfBoundsException when I set SetCacheable to true.

This is the stacktrace:

at NHibernate.Type.TypeFactory.Disassemble(Object[] row, ICacheAssembler[] types, Boolean[] nonCacheable, ISessionImplementor session, Object owner)
   at NHibernate.Cache.StandardQueryCache.Put(QueryKey key, ICacheAssembler[] returnTypes, IList result, Boolean isNaturalKeyLookup, ISessionImplementor session)
   at NHibernate.Loader.Loader.PutResultInQueryCache(ISessionImplementor session, QueryParameters queryParameters, IType[] resultTypes, IQueryCache queryCache, QueryKey key, IList result)
   at NHibernate.Loader.Loader.ListUsingQueryCache(ISessionImplementor session, QueryParameters queryParameters, ISet`1 querySpaces, IType[] resultTypes)
   at NHibernate.Loader.Loader.List(ISessionImplementor session, QueryParameters queryParameters, ISet`1 querySpaces, IType[] resultTypes)
   at NHibernate.Loader.Custom.CustomLoader.List(ISessionImplementor session, QueryParameters queryParameters)
   at NHibernate.Impl.SessionImpl.ListCustomQuery(ICustomQuery customQuery, QueryParameters queryParameters, IList results)
   at NHibernate.Impl.SessionImpl.List(NativeSQLQuerySpecification spec, QueryParameters queryParameters, IList results)
   at NHibernate.Impl.SessionImpl.List[T](NativeSQLQuerySpecification spec, QueryParameters queryParameters)
   at NHibernate.Impl.SqlQueryImpl.List[T]()
   at ...

Can anyone help me and say how I can fix this (or even if this is supported in NHibernate)?

I'm currently using NHibernate-version 2.1.0.4000.

Thank you, Jelle

+1  A: 

I may be mistaken, but I think that the query cache relies on the second level cache being enabled. To be honest, I wonder if it would be all that useful without entity caching in the second level cache, because all the it stores is a list of ID's associated with a given query / parameter combination (you'd still need to go to the database to actually get the objects, and in some situations this could lead to 1 query per object).

This is one of the better posts on caching in nhibernate that I've seen, and it seems to suggest towards the end that you can only cache queries in the second level cache

AlexCuse