沉睡森林@漂在北京

          本處文章除注明“轉(zhuǎn)載”外均為原創(chuàng),轉(zhuǎn)載請注明出處。

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            152 隨筆 :: 4 文章 :: 114 評論 :: 0 Trackbacks
          1.  首先設(shè)置EhCache,建立配置文件ehcache.xml,默認(rèn)的位置在class-path,可以放到你的src目錄下:
          xml version="1.0" encoding="UTF-8"?>  
          <ehcache>  
          <diskStore path="java.io.tmpdir"/>    
          <defaultCache  
          maxElementsInMemory="10000"   
          eternal
          ="false"   
            
          overflowToDisk
          ="true"   
            
          timeToIdleSeconds
          ="300"   
          timeToLiveSeconds
          ="180"   
          diskPersistent
          ="false"  
          diskExpiryThreadIntervalSeconds
          = "120"/>    
          ehcache>  
            2.  在Hibernate配置文件中設(shè)置:

          <hibernate-configuration>
          <session-factory>……<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property><property name="cache.use_second_level_cache">true</property>……</session-factory></hibernate-configuration>
                  此外,可以把cache.use_second_level_cache設(shè)置為false關(guān)閉所有的hibernate二級緩存。但此屬性對指定
          <cache>的類缺省為true。
           
             3.  為了使用二級緩存,需要在每一個(gè)Hibernate Entity上配置。
          @Entity  
          @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)   
          public class Forest {  }  
          @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)   
          @JoinColumn(name="CUST_ID")   
          @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)   
          public SortedSet getTickets() {   
              return tickets;   
          }  
          @Cache(   
              CacheConcurrencyStrategy usage();                 (1)   
              String region() default "";                       (2)   
              String include() default "all";                   (3)   
          )  
          (1) usage: 提供緩存對象的事務(wù)隔離機(jī)制,可選值有以下幾種
          (NONE, READ_ONLY, NONSTRICT_READ_WRITE, READ_WRITE, TRANSACTIONAL)
          (2) region (optional): 指定緩存的區(qū)域,默認(rèn)是類的全限定名。利用緩存區(qū)域,可以更精確的指定每個(gè)區(qū)域的緩存超前策略。如果指定了緩存區(qū)域前綴(在hibernate.cfg.xml中設(shè)置cache.region_prefix屬性為一個(gè)字符串),則所有的緩存區(qū)域名前將加上這個(gè)前綴。
          (3) include (optional): all to include all properties, non-lazy to only include non lazy properties (default all).
          如果不是使用annotation的話,則是在Hbm文件中添加cache usage="read-only"
          posted on 2010-03-14 17:46 王總兵 閱讀(598) 評論(1)  編輯  收藏

          評論

          # re: Hibernate Annotation 中配置EhCache緩存 2011-08-03 23:09 we are people
          不能發(fā)政治相關(guān)的內(nèi)容------我要投票權(quán)!  回復(fù)  更多評論
            


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 吉木乃县| 莆田市| 台湾省| 德清县| 房产| 工布江达县| 蕉岭县| 榆社县| 宝兴县| 潍坊市| 海阳市| 资溪县| 昂仁县| 洱源县| 大关县| 驻马店市| 米林县| 修文县| 大悟县| 平舆县| 鄂温| 辰溪县| 沅江市| 广宗县| 秦安县| 安阳市| 凌海市| 兴隆县| 邓州市| 息烽县| 玛曲县| 东乌珠穆沁旗| 道孚县| 吐鲁番市| 漳平市| 富平县| 门头沟区| 赞皇县| 榆社县| 盘山县| 乌拉特中旗|