Hibernate第二级缓存<< net.sf.ehcache.hibernate.EhCacheProvider>> [英] Hibernate Second level Cache <<net.sf.ehcache.hibernate.EhCacheProvider>>
问题描述
我想在我的hibernate Project中使用二级缓存,但我只知道一点关于hibernate二级缓存的知识,任何人都可以解释我在代码中如何使用它以及我需要什么配置和.jar文件?
我将这些设置设置为我的hibernate.cfg.xml文件
< property name =hibernate.cache。 use_query_cache>真< /性>
< property name =hibernate.cache.use_second_level_cache> true< / property>
< property name =hibernate.cache.provider_class> net.sf.ehcache.hibernate.EhCacheProvider< / property>
并添加这些jar文件 ehcache-1.6.1.jar,ehcache- 1.6.1-javadoc.jar,ehcache-1.6.1-sources.jar
我想知道我应该改变其他配置吗?
如何才能理解我的项目使用二级缓存?如果只是放置这个设置,hibernate会自动使用这个或我必须在我的ant中使用其他代码.java类(像任何注解或其他)
您正在寻找的注释是 org.hibernate.annotations.Cache 。基本用法是:
$ p $ @Entity
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public MyEntity {
...
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public List< ElementType> getSomeCollection(){
...
}
}
对于查询,您需要启用查询缓存通过设置 所有这些都说明了,你需要非常小心缓存和真正理解你在做什么,否则它会造成更多的伤害比帮助。不要把它看作是快速修复 - 例如缓存所有内容,肯定是错误的做法。 I want use second level cache in my hibernate Project but I just know a little about hibernate second level cache, can any one explain how shoud I use this in my code and what configuration and .jar file I need?
I set these setting to my hibernate.cfg.xml file and add these jar file and how can I understand that my project uses Second level cache? if just put this setting, hibernate automatically use this or I must use ant other code in my .java class(like any annotation or something else) The annotation you're looking for is org.hibernate.annotations.Cache. Basic usage is: For queries, you need to enable query cache by setting All that said, you need to be really careful with caching and REALLY UNDERSTAND what you're doing, otherwise it'll do more harm than help. Don't look at it as "quick fix" - caching everything, for example, is definitely the wrong thing to do. 这篇关于Hibernate第二级缓存<< net.sf.ehcache.hibernate.EhCacheProvider>>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! hibernate.cache.use_query_cache
属性为true,并指定查询在其声明中可缓存(对于命名查询)或通过调用 setCacheable(true)
在查询实例上。
<property name="hibernate.cache.use_query_cache">true</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</property>
ehcache-1.6.1.jar, ehcache-1.6.1-javadoc.jar, ehcache-1.6.1-sources.jar
I want know shoud I change any other configuration?@Entity
@Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public MyEntity {
...
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public List<ElementType> getSomeCollection() {
...
}
}
hibernate.cache.use_query_cache
property to true AND specify that query is cacheable in its declaration (for named queries) or by calling setCacheable(true)
on query instance.