Posted on 2007-12-07 15:36 
G_G 阅读(970) 
评论(0)  编辑  收藏  所属分类: 
hibernate 
			 
			
		 
		在hibernate.cfg.xml 中添加缓存  t1oo 一对多 t2oo (t2ooSet)
    <property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
    <property name="hibernate.cache.use_query_cache">true</property>
    <mapping resource="hbn/bean/T1oo.hbm.xml" />
    <mapping resource="hbn/bean/T2oo.hbm.xml" />
    <class-cache class="hbn.bean.T1oo" usage="read-only" />
    <collection-cache collection="hbn.bean.T1oo.t2ooSet" usage="read-only" />
    <class-cache class="hbn.bean.T2oo" usage="read-only" />
在src根目录下 ehcache.xml 
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
    <diskStore path="java.io.tmpdir"/>
    <defaultCache
        maxElementsInMemory="10000" //最大缓存数目
        eternal="false"<!-- 缓存是否持久 -->
        timeToIdleSeconds="120" <!-- 当缓存闲置n秒后销毁 -->
        timeToLiveSeconds="120"<!-- 当缓存存活n秒后销毁-->
        overflowToDisk="true"<!-- 是否保存到磁盘,当系统当机时-->
        diskPersistent="false"
        diskExpiryThreadIntervalSeconds="120"/>
    <cache
     name="hbn.bean.T1oo"
     maxElementsInMemory="450"
     eternal="false"
     timeToLiveSeconds="600"
     overflowToDisk="true"/>
</ehcache>
测试:
    public void testCa()throws Exception{
        System.out.println( getT1ooAll() );
        
        Thread.sleep(2*1000);
        
        System.out.println( getT1ooAll() );
    }
控制台输出
Hibernate: select t1oo0_.id as id, t1oo0_.name as name0_ from t1oo t1oo0_ limit ?
Hibernate: select t2ooset0_.aid as aid1_, t2ooset0_.id as id1_, t2ooset0_.id as id0_, t2ooset0_.version as version1_0_, t2ooset0_.avg as avg1_0_, t2ooset0_.aid as aid1_0_ from t2oo t2ooset0_ where t2ooset0_.aid=?
Hibernate: select t2ooset0_.aid as aid1_, t2ooset0_.id as id1_, t2ooset0_.id as id0_, t2ooset0_.version as version1_0_, t2ooset0_.avg as avg1_0_, t2ooset0_.aid as aid1_0_ from t2oo t2ooset0_ where t2ooset0_.aid=?
24 : 23 : 25 : 2
//在这缓存成功 没向数据库提交 sql语句 
24 : 23 : 25 : 2