1.配置ehcache.xml文件放到path下:
<?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)不要使用默认缓存Cache策略defaultCache(多个共享)
(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>
< 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 ="uuid"></generator>
</id>
<property name="field1" type="java.lang.String" />
<property name="field2" type="java.lang.String" />
</>
</hibernate-mapping>
如果使用Annocation则类前添加
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)