程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> hibernate3二級緩存的配置及測試

hibernate3二級緩存的配置及測試

編輯:關於JAVA

1.配置ehcache.xml文件,放到classpath下:

<?xml version="1.0" encoding="GBK"?>
   <ehcache>
       <diskStore path="D:\\TempObject"/>
       <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="100"
            timeToLiveSeconds="1000"
           overflowToDisk="true"
       />
       <cache name="com.sitechasia.occ.core.base.ExampleForTest" maxElementsInMemory="10000"
           eternal="false"
           timeToIdleSeconds="100"
           timeToLiveSeconds="1000"
           overflowToDisk="true"
        />
  </ehcache>

建議自定義cache時,cache名字和類路徑名相同。

(1)不要使用默認緩存策略defaultCache(多個class共享)

(2)不要給cache name另外起名

否則繼承AbstractTransactionalDataSourceSpringContextTests做測試時,拋出

org.hibernate.cache.CacheException: java.lang.IllegalStateException: The com.sitechasia.occ.core.base.ExampleForTest Cache is not alive.(我注釋了紅色的cache,使用defaultCache導致)

2.在ExampleForTest.hbm.xml中添加:(如果有集合,也需要添加)

<hibernate-mapping>
  <class name="com.sitechasia.occ.core.base.ExampleForTest"
    table="TESTTABLE" lazy="false">
    <cache usage="read-write"/>
    <id name="id" type="java.lang.String">
      <column name="id" length="32" />
      <generator class="uuid"></generator>
    </id>
    <property name="field1" type="java.lang.String" />
    <property name="field2" type="java.lang.String" />
  </class>
</hibernate-mapping>

如果使用Annocation,則類前添加

@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved