English 中文(简体)
Nhebertenate 检索缓存查询结果, 尽管按条款排列的 ORDER 不同
原标题:NHibernate retrieves cached query results even though the ORDER BY clause differs

我看到了在Nhibertnate里我所谓的窃听器 里面有查询和命令条款...

当我运行下面的查询时...

SELECT this_.Id          as y0_,
       this_.Name        as y1_
FROM   Products this_
WHERE  this_.IsActive = 1
ORDER BY this_.IsPremium desc

...Nhebertate 成功缓存了结果, 如果我有查询缓存打开, 我告诉它缓存这个查询( 例如使用 < code> 标准. setCaxable( true) ) 。

不幸地,作为美妙的""http://nhprof.com/"rel="nofollow">NHProf 告诉我,NHebertate在运行此查询时也使用缓存查询结果:

SELECT this_.Id          as y0_,
       this_.Name        as y1_
FROM   Products this_
WHERE  this_.IsActive = 1
ORDER BY this_.IsPremium desc,
         this_.Name

有人能解释为什么或指点点我看一些关于这个“特点”的深入文件吗? 或者,更糟糕的是,有人对问题有“强烈”的解决方案吗?

问题回答

Nhebertate 使用第一层的缓存( 由身份地图执行) 来隐藏您查询的每个对象。 Nhebertate 在单个实体中工作, 即使您查询了对象列表 。

Let me explain some stuff:
you query if you execute this query and try to get entity with id 1:

session.get<SomeEntity>(1);

Nhebertate 将检查缓存以查看它是否已经包含 ID 1 。 如果是的话, 缓存对象将被返回, 它将不会运行查询 。 否则, 询问将被执行, 选择记录, 把它放在缓存中并返回给您 。 如果您再次执行此查询, 它会被缓存, 并且该对象将被返回而不进行新的查询 。

现在,如果您询问这样的列表 :

session.QueryOver<SomeEntity>().List();

nhenate 不知道哪些 id s 将会被抓取, 因此它会查询所有记录, 并对照缓存检查结果, 即使您两次运行查询。 假设您在数据库中有两个记录( ID 1 和 2 ) 。 您在缓存仍然空的情况下, 将它们与查询一起取回 。 两种记录都会被抓取, 放在缓存中并返回到您。 现在您插入记录3、4 和 5, 在您输入记录时, 您也会更新记录 1 。 现在, 如果您再次运行查询, 它将会读取所有 5 个记录, 但是现在也可以缓存记录3、4 和 5 。 您将会收到一份列表, 5个对象中有3 、 4 、 5个是刚刚读取的, 但1 和 2 的缓存版本将被退回。 您将不会得到 ID 1 的更新版本 。

因此可以回答您的问题: 更改顺序并不重要。 您的查询将产生一组记录, 逐个对照缓存进行检查, 如果其中之一已经存在, 则返回这个缓存版本 。

解决问题的办法可以是:

  • using session.Refresh(obj); if you know which ones have been updated.
  • evict some or all entities from the session (which means they ll be thrown out of the cache and can be refetched with a query). note: if you evict them, changed won t be saved.
  • you can use a stateless session, which has - as the stateless name suggests - no cache.




相关问题
nHibernate one-to-many inserts but doesnt update

Instead of getting into code, I have a simple question. Default behavior for a simple one-to-many is that it inserts the child record then updates the foreign key column with the parent key. Has ...

How Do I copy an existing nhibernate object as a new object?

I a persisted NHibernate object that I would like to repersist as a new entity. How do I get NHibernate to save this object as if it was a new? I am thinking I might create a session interceptor to ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

NHibernate Search in a List using ICriteria

I have my class X : public class ClassX { public virtual IList<ClassY> ListY { get; set; } ... } My ClassX mapping (using Fluent) ... HasMany<ClassX>(x => x.ListY ) ....

热门标签