使用复合主键时,NHibernate二级缓存不起作用? [英] NHibernate second level cache not working when using composite primary key?

查看:69
本文介绍了使用复合主键时,NHibernate二级缓存不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在NHibernate中使用复合主键的合适方法是什么,以便可以进行缓存?

What's the proper way to use a composite primary key in NHibernate so it will be amenable for caching?

我隔离了类似于本文最后部分的组合主键:

I isolate the composite primary key similar to the last part of this post: http://devlicio.us/blogs/anne_epstein/archive/2009/11/20/nhibernate-and-composite-keys.aspx

但是二级缓存没有缓存它.

But the second level cache isn't caching it.

如果用于代理密钥,则表示正在缓存,例如

If for surrogate key, it is caching, e.g.

 var q = from p in session.Query<Product>()
         select p;

 q.Cacheable().ToList(); // hit database


 // this doesn't hit the database, Get get its value from cache(via last query)
 var px = secondSession.Get<Product>(1); 

但是当使用复合主键时,Get不会从缓存中获取其值:

But when using composite primary key, the Get doesn't get its value from the cache:

 var q = from pl in session.Query<ProductLanguage>()
         select pl;      

  q.Cacheable().ToList(); // hit the database

  // this hits the database, Get didn't get its value from cache(via last query)
  var plx = secondSession.Get<ProductLanguage>(
         new ProductLanguageCompositeKey { ProductId = 1, LanguageCode = "en" });

复合键(这里是ProductLanguageCompositeKey),甚至没有将其隔离为自己的类(具有Seri​​alizable属性,Equals和GetHashCode)吗?

Is composite key(ProductLanguageCompositeKey here), even isolated it its own class(with Serializable attribute, Equals and GetHashCode) doesn't get cached?

我们如何使通过组合键访问的实体可缓存?

How can we make an entity accessed via composite key cacheable?

推荐答案

对于那些怀疑使用复合主键时NHibernate的二级缓存不起作用的人(缓存有效),请检查复合主键的值是否为原始格式.解决我的缓存问题的方法:

For those suspecting if NHibernate's second level cache is not working when using composite primary key (caching works), check if your composite primary key's values are in pristine form. Solution to my caching problem:

查看全文

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