沉睡森林@漂在北京

          本處文章除注明“轉載”外均為原創,轉載請注明出處。

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            152 隨筆 :: 4 文章 :: 114 評論 :: 0 Trackbacks
          1.  首先設置EhCache,建立配置文件ehcache.xml,默認的位置在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配置文件中設置:

          <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設置為false關閉所有的hibernate二級緩存。但此屬性對指定
          <cache>的類缺省為true。
           
             3.  為了使用二級緩存,需要在每一個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: 提供緩存對象的事務隔離機制,可選值有以下幾種
          (NONE, READ_ONLY, NONSTRICT_READ_WRITE, READ_WRITE, TRANSACTIONAL)
          (2) region (optional): 指定緩存的區域,默認是類的全限定名。利用緩存區域,可以更精確的指定每個區域的緩存超前策略。如果指定了緩存區域前綴(在hibernate.cfg.xml中設置cache.region_prefix屬性為一個字符串),則所有的緩存區域名前將加上這個前綴。
          (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
          不能發政治相關的內容------我要投票權!  回復  更多評論
            


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


          網站導航:
           
          主站蜘蛛池模板: 托克托县| 手游| 汝城县| 高台县| 白沙| 田阳县| 东丽区| 冕宁县| 平谷区| 阿拉善盟| 林西县| 永城市| 九寨沟县| 图木舒克市| 新沂市| 徐汇区| 宿松县| 高淳县| 和龙市| 互助| 鹤庆县| 宜春市| 邮箱| 东兰县| 泰和县| 藁城市| 茶陵县| 德阳市| 吉水县| 临高县| 调兵山市| 雷山县| 汽车| 右玉县| 枣阳市| 深水埗区| 南陵县| 韶山市| 剑阁县| 边坝县| 同德县|