首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

hibernate开启缓存的有关问题

2012-10-29 
hibernate开启缓存的问题项目构架:spring+hibernate+webwork1:配置spring的applicationContext-hibernate.

hibernate开启缓存的问题

项目构架:spring+hibernate+webwork

1:

配置spring的applicationContext-hibernate.xml文件

加入:

??? <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>??
????<prop key="hibernate.cache.use_query_cache">true</prop>?
????<prop key="hibernate.cache.use_second_level_cache">true</prop>

?

?

2:

在工程目录下建立 ehcache.xml hibernate的缓存配置文件,编译后位于classes目录下

ehcache.xml内容如下:

<?xml version="1.0" encoding="UTF-8"?>?????
<ehcache>?
??? <diskStore path="java.io.tmpdir"/>?
??? <defaultCache?
??????? maxElementsInMemory="10000"?
??????? eternal="false"?
??????? timeToIdleSeconds="120"?
??????? timeToLiveSeconds="120"?
??????? overflowToDisk="true"/>?
??????????
???? <cache? name="org.hibernate.cache.StandardQueryCache"?????????
????????? maxElementsInMemory="10000"?????????
????????? eternal="false"?????????
????????? timeToIdleSeconds="200"?????????
????????? timeToLiveSeconds="500"?????????
????????? overflowToDisk="true"/>?
???????????????
???? <cache name ="org.hibernate.cache.UpdateTimestampsCache"???
???????? maxElementsInMemory ="5000"?
???????? eternal ="true"???
???????? overflowToDisk ="true"/>???
???????
</ehcache>

?

3:在hibernate生成的hbm.xml文件中,把要使用缓存的po对象,加入

?

<cache usage="read-only"/><!-- read-write -->

必须加在

<id name="pkid" type="java.lang.Integer">

主键id前面。

?

4:此时配置完成了,如果你使用getHibernateTemplate().find(hql)语句查询时候,默认还不是使用缓存

在find之前加入:getHibernateTemplate().setCacheQueries(true)才能正常使用你配置好的缓存

你查询的hql对象必须定义了使用缓存,也就是hbm文件存在<cache usage="read-only"/>这样的内容。

?

如果你使用getHibernateTemplate().load(id)方法查询,则不需要设置开启缓存了,该方法模式开启缓存。

自己写hql语句的需要开启缓存,默认是不使用缓存的,这点要注意。

热点排行