连接查询的二级缓存问题 [英] 2nd level cache problem with join query

查看:121
本文介绍了连接查询的二级缓存问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用二级缓存为我的查询与加载(查询下面写了3种不同的方式,我使用查询缓存)。我有标准的一对多协会。我为parent,child和parent与class之间的关联设置了实体缓存。第二级缓存不起作用,因为我有异常。



我以三种不同的方式写了我的查询:

Criteria: />

  session.CreateCriteria< DictionaryMaster>()。SetFetchMode(DictionaryItems,FetchMode.Eager)
。 SetResultTransformer(new DistinctRootEntityResultTransformer())
.SetCacheable(true).SetCacheMode(CacheMode.Normal)
.List< DictionaryMaster>()。ToList();

当我调用这个查询时,我得到异常Unable to perform find [SQL:SQL not available]
我认为存在的问题是因为我使用DistinctRootEntityResultTransformer转换。

$ p $ b查询结束:


$我将开始创建我的自定义转换类,我希望这将工作。 b $ b

  session.QueryOver< DictionaryMaster>()。Fetch(x => x.DictionaryItems).Eager 
.TransformUsing(new DistinctRootEntityResultTransformer())
.Cacheable().CacheMode(CacheMode.Normal)
.List< DictionaryMaster>()。ToList();

例外与Criteria相同。



Linq:

  session.Query< DictionaryMaster>()。Fetch(x => x.DictionaryItems).Cacheable ).CacheMode(CacheMode.Normal).ToList(); 

这里错误取决于版本的nhibernate在3.1中有一个错误 https://nhibernate.jira.com/browse/ NH-2587?page = com.atlassian.jira.plugin.system.issuetabpanels%3Achangehistory-tabpanel
但在3.2版本中,我得到了这个: https://nhibernate.jira.com/browse/NH-2856



前进

解决方案

sJHony我找到了解决方案,但对我来说更像是解决方法。因此,如果您知道任何其他解决方案,请给我签名。

我使用QueryOver 而不进行任何转换,这个解决方案的缺点是查询返回的元素数量相等。接下来,我使用distinct检索内存中不乘的列表。

这个解决方案是可以的,但是当我们为查询添加一个Fetch时,对象中的集合也会相乘,这就是为什么我把集合类型从IList(Bag)修改为ISet(Set) / p>

代码如下:

  var queryCacheResult = 
session.QueryOver< DictionaryMaster>()
.Fetch(x => x.DictionaryItems).Eager
.Cacheable().CacheMode(CacheMode.Normal)
.List< DictionaryMaster>()ToList();

return queryCacheResult.Distinct(new KeyEqualityComparer< DictionaryMaster>(x => x.Code))。ToList();


I want to use Second level Cache for my query with eager loading(query is below wrote in 3 different ways, i use query cache). I have standard one to many association. I set entity cache for parent, child, and association between parent and class. And the 2nd level cache doesn't work because i got exceptions.

I wrote my query in 3 different way:
Criteria:

 session.CreateCriteria<DictionaryMaster>().SetFetchMode("DictionaryItems", FetchMode.Eager)
                .SetResultTransformer(new DistinctRootEntityResultTransformer())
                .SetCacheable(true).SetCacheMode(CacheMode.Normal)
                    .List<DictionaryMaster>().ToList();

When I invoked this query i got exception "Unable to perform find[SQL: SQL not available]" I think that the problem exists because I use DistinctRootEntityResultTransformer transform. I will start create my custom transform class and I hope that will work.


Query over:

 session.QueryOver<DictionaryMaster>().Fetch(x => x.DictionaryItems).Eager
                    .TransformUsing(new DistinctRootEntityResultTransformer())
                    .Cacheable().CacheMode(CacheMode.Normal)
                    .List<DictionaryMaster>().ToList();

Exception is the same as in Criteria.

Linq:

session.Query<DictionaryMaster>().Fetch(x => x.DictionaryItems).Cacheable().CacheMode(CacheMode.Normal).ToList();

Here error depends from the version of nhibernate in 3.1 a got this error https://nhibernate.jira.com/browse/NH-2587?page=com.atlassian.jira.plugin.system.issuetabpanels%3Achangehistory-tabpanel but in 3.2 version i got this: https://nhibernate.jira.com/browse/NH-2856

Thanks in advance

解决方案

sJHony I have found solution, but for me is more like workaround. Therefore if you know any other solution give me sign.
I utilize QueryOver without any transformation, the fault of this solution is that the query return elements equal amount of childs. Next I retrieve not multiplied list in memory using distinct.
This solution is ok, but when we add one more Fetch for query, then collection in object also be multiplied, that is why i modified collection type from IList(Bag) to ISet(Set).


Code looks like:

var queryCacheResult =
                session.QueryOver<DictionaryMaster>()
                .Fetch(x => x.DictionaryItems).Eager
                    .Cacheable().CacheMode(CacheMode.Normal)
                    .List<DictionaryMaster>().ToList();

            return queryCacheResult.Distinct(new KeyEqualityComparer<DictionaryMaster>(x => x.Code)).ToList();

这篇关于连接查询的二级缓存问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆