查询Hibernate缓存而不是数据库 [英] Query Hibernate Cache instead of database

查看:169
本文介绍了查询Hibernate缓存而不是数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样一个查询:

 选择* FROM table1 WHERE name LIKE's%'; 

我不希望这个查询从数据库中获取数据;相反,它应该从休眠会话或其他东西返回数据。我认为启用二级缓存有助于但不确定它是否有助于筛选查询。



如何强制查询不从数据库获取数据?

解决方案

Hibernate的第一级缓存是Session级缓存,所以如果对象目前在Hibernate会话结果中将会从中获取结果。 p>

二级缓存是一个SessionFactory级缓存,因此可以为任何用户缓存结果。



只要我知道你需要缓存一个特定的查询。休眠也有这个功能。 org.hibernate.Query.setCacheable(true)可以在这里使用。



从文档


$ b


为特定查询启用结果缓存



而不是
从缓存结果中受益,则需要在启用查询缓存整体后为
个别查询启用缓存。要
为特定查询启用结果缓存,请调用
org.hibernate.Query.setCacheable(true)。这个调用允许查询
查找现有的缓存结果,或者在
执行时将其结果添加到缓存中。


另见

Hibernate缓存


I have a query like:

Select * FROM table1 WHERE name LIKE 's%';

I don't want this query to fetch data from database; instead it should return data from hibernate session or something else. I think enabling second level cache will help but not sure that it will help in filtered queries.

How can I force a query not to fetch data from database?

解决方案

Hibernate first level cache is Session level cache, so if the object is currently in the Hibernate session results will be fetched from it.

Second level cache is a SessionFactory level cache, so the result fill be cached for any user.

AS far as i understand you need cache for a specific query. Hibernate has also this feature. org.hibernate.Query.setCacheable(true) can be used here.

From the documentation

Enable results caching for specific queries

Since most queries do not benefit from caching of their results, you need to enable caching for individual queries, e ven after enabling query caching overall. To enable results caching for a particular query, call org.hibernate.Query.setCacheable(true). This call allows the query to look for existing cache results or add its results to the cache when it is executed.

See also

Hibernate Caching

这篇关于查询Hibernate缓存而不是数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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