Hello,
1. Download ehcache.jar and put it in your classpath
2. In hibernate.cfg.xml, declare ehcache as your cache provider using the hibernate.cache.provider_class property (hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider). If you use spring, set the property in the 'hibernateProperties' of the session factory declaration.
3. In your hbm file, declare caching as follow :
<class
name="eg.MyClass"
table="MY_TABLE"
mutable="false"
>
<cache usage="read-only" />
.....
</class>
or
<class
name="eg.MyClass"
table="MY_TABLE"
>
<cache usage="read-write" />
.....
</class>
4. Create ehcache.xml in your classpath and write some cache region :
<ehcache>
<diskStore path="java.io.tmpdir"/>
<defaultCache
maxElementsInMemory="0"
eternal="false"
timeToIdleSeconds="0"
timeToLiveSeconds="0"
overflowToDisk="false"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="0"
memoryStoreEvictionPolicy="LFU"
/>
<cache name="eg.MyClass"
maxElementsInMemory="25"
eternal="false"
timeToIdleSeconds="86400"
timeToLiveSeconds="86400"
overflowToDisk="false"
memoryStoreEvictionPolicy="LFU"
/>
</ehcache>
5. In your log4j.xml, set the log level for ehcache :
<logger name="org.hibernate.cache" additivity="true">
<level value="info" />
</logger>
<logger name="net.sf.ehcache" additivity="true">
<level value="info" />
</logger>
Hope this helps !
|