按条件获取实体时不使用类缓存 [英] Class-cache not used when getting entity by criteria

查看:78
本文介绍了按条件获取实体时不使用类缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在nhibernate.cfg.xml文件中设置了类缓存.

I've set class-cache in nhibernate.cfg.xml file.

当我通过ID获取我的实体时,加载对象后就看不到SQL请求.

When I get my entity by Id, I don't see SQL requests once object is loaded.

但是当我使用条件获取实体时,总会有SQL查询...

But when I fetch entity using criteria, there is always SQL queries...

我猜这回答了我的问题:

I guess this answers my question:

http://www.javalobby.org/java/forums/t48846.html

比方说,我们希望基于更复杂的内容来查找条目 查询要比直接通过ID(例如名称)直接查询.在这种情况下,休眠 必须仍然发出SQL语句以获取 询问.因此,例如,这段代码:

Let's say that we wanted to lookup entries based on a more complex query than directly by ID, such as by name. In this case, Hibernate must still issue an SQL statement to get the base data-set for the query. So, for instance, this code:

Query query = session.createQuery("from Person as p其中 p.firstName =?); query.setString(0," John);列表l = query.list(); ...将调用一个选择(假设我们的关联是 缓存).

Query query = session.createQuery("from Person as p where p.firstName=?"); query.setString(0, "John"); List l = query.list(); ... would invoke a single select (assuming our associations were cached).

从* firstName ='John'的人中选择*此单选将 然后返回"1",然后缓存将用于所有其他查找 因为我们缓存了所有内容.这个强制性的单选是 查询缓存进来了.

select * from Person where firstName='John' This single select will then return '1', and then the cache will be used for all other lookups as we have everything cached. This mandatory single select is where the query cache comes in.

推荐答案

不确定"编辑"是否表示已找到所有答案. NHibernate将缓存机制分为两个区域.

Not sure if your "edit" means that you've found all answers. NHibernate splits the caching mechanism into two areas.

第一个是类"缓存,如上所述.对具有特定ID(Session.Get(id);、引用的属性,集合)的对象的任何请求,都可以使用类缓存.

The First is the "class" cache, as mentioned above. Any request for an object with specific ID (Session.Get(id);, referenced property, collection), the class cache could be used.

第二个是查询"缓存.在这种情况下,用于缓存的取决于传递的Criteria.这些用于获取结果集(位置,排序依据,顶部...).在这种情况下,缓存的是该查询返回的一组ID.

The Second is the "query" cache. In that case, the key used for cache depends on passed Criteria. Those were used to get the result set (where, order by, top ...). The value cached in this case is a set of IDs, returned by that query.

因此,稍后,当应用相同的条件时,将返回一组高速缓存的ID,并重用来自类高速缓存(按ID)的高速缓存的实体.

So later, when the same criteria are applied, cached set of IDs is returned and cached entities from a class-cache (by ID) are reused.

(特定情况适用于投影,在最新的NHibernate版本中也可以缓存该投影.在这种情况下,结果不包含具有唯一ID的实体,而是一组列.虽然不适合ID基于缓存的方法,它也可以正常工作.版本3.3为我完成了此操作)

所以,最后要回答:

但是当我使用条件获取实体时,总会有SQL查询...

But when I fetch entity using criteria, there is always SQL queries...

我们还必须允许查询缓存:

we have to allow the query cache as well:

<property name="cache.use_second_level_cache">true</property>
<property name="cache.use_query_cache">true</property>

允许二级缓存,并且可以存储具有ID的对象.此外,还启用了对查询缓存的支持,因此完全不应该将具有相同Criteria组合的第二个调用发送到SQL Server.

Second level cache is allowed and objects with ID can be stored. Also the support for query cache is enabled, so the second call of the same Criteria combination should not go to SQL server at all.

缓存查询示例:

criteria.SetCacheable(true)
    .SetCacheMode(CacheMode.Normal)
    .SetCacheRegion("LongTerm");

这篇关于按条件获取实体时不使用类缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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