2

我有一个实体:

public class SalesUnit
{
    public virtual long Id { get; set; }
    public virtual string Name { get; set; }

}

和相关的Dto:

public class SalesUnitDto
{
    public long Id { get; set; }
    public string Name { get; set; }

}

我有一个非常简单的查询:

SalesUnitDto result = null; 
var list = _session.QueryOver<SalesUnit>()
                .SelectList(l => l
                    .Select(x => x.Id).WithAlias(() => result.Id)
                    .Select(x => x.Name).WithAlias(() => result.Name))
                .TransformUsing(Transformers.AliasToBean<SalesUnitDto>())
                //.Cacheable()
                .List<SalesUnitDto>();

它一直在工作,直到我插入二级缓存。因此,如果我取消注释Cacheable()行,我将得到异常:

消息:值不能为空。参数名称:别名 StackTrace:

   at NHibernate.Transform.AliasedTupleSubsetResultTransformer.IncludeInTransform(String[] aliases, Int32 tupleLength)
   at NHibernate.Transform.CacheableResultTransformer.Create(ITupleSubsetResultTransformer transformer, String[] aliases, Boolean[] includeInTuple)
   at NHibernate.Loader.Loader.GenerateQueryKey(ISessionImplementor session, QueryParameters queryParameters)
   at NHibernate.Loader.Loader.ListUsingQueryCache(ISessionImplementor session, QueryParameters queryParameters, ISet`1 querySpaces, IType[] resultTypes)
   at NHibernate.Impl.SessionImpl.List(CriteriaImpl criteria, IList results)

那有什么问题呢?它是 NHibernate 的错误吗?我尝试了不同的提供商,但无济于事。我也尝试像这样创建 CacheableResultTransformer:

CacheableResultTransformer.Create(Transformers.AliasToBean<SalesUnitDto>(), new[] { "Id", "Name" }, new[] { true, true })

它可以返回和缓存数据,但只能作为元组(对象 [])。我没有设法返回 Dto。

这是演示问题的工作示例:github

4

2 回答 2

2

事实证明这是(N)Hibernate 的错误/限制。当缓存被激活时,原始IResultTransformer开始接收null string[] aliases参数,这对于AliasToBeanTransformer实现来说是必不可少的,因此你得到了异常。

作为一种解决方法,我可以建议使用以下自定义扩展方法和转换器,它在调用时存储当前别名,并AliasToBeanTransformer在传递的参数为时将它们传递给底层null

public static class NHExtensions
{
    public static IQueryOver<TRoot, TSubType> TransformUsingAliasToBean<TRoot, TSubType>(this IQueryOver<TRoot, TSubType> query, Type resultType)
    {
        ITupleSubsetResultTransformer resultTransformer = new AliasToBeanResultTransformer(resultType);
        var criteria = query.UnderlyingCriteria as NHibernate.Impl.CriteriaImpl;
        if (criteria != null)
            resultTransformer = new CacheableAliasToBeenResultTransformer(resultTransformer, criteria.Projection.Aliases);
        return query.TransformUsing(resultTransformer);
    }

    class CacheableAliasToBeenResultTransformer : ITupleSubsetResultTransformer
    {
        ITupleSubsetResultTransformer baseTransformer;
        string[] aliases;

        public CacheableAliasToBeenResultTransformer(ITupleSubsetResultTransformer baseTransformer, string[] aliases)
        {
            this.baseTransformer = baseTransformer;
            this.aliases = aliases;
        }

        public bool[] IncludeInTransform(string[] aliases, int tupleLength)
        {
            return baseTransformer.IncludeInTransform(aliases ?? this.aliases, tupleLength);
        }

        public bool IsTransformedValueATupleElement(string[] aliases, int tupleLength)
        {
            return baseTransformer.IsTransformedValueATupleElement(aliases ?? this.aliases, tupleLength);
        }

        public IList TransformList(IList collection)
        {
            return baseTransformer.TransformList(collection);
        }

        public object TransformTuple(object[] tuple, string[] aliases)
        {
            return baseTransformer.TransformTuple(tuple, aliases ?? this.aliases);
        }
    }
}

你的查询是:

SalesUnitDto result = null; 
var list = _session.QueryOver<SalesUnit>()
    .SelectList(l => l
        .Select(x => x.Id).WithAlias(() => result.Id)
        .Select(x => x.Name).WithAlias(() => result.Name))
    .TransformUsingAliasToBean(typeof(SalesUnitDto))
    .Cacheable()
    .List<SalesUnitDto>();

已针对此场景进行了测试和工作。当然,我不能保证它适用于所有QueryOver变体。

于 2016-12-15T19:55:51.723 回答
1

NHibernate 的这个 bug 已在 v4.1.0.4000中修复!

于 2016-12-26T11:47:46.067 回答