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)