Using the EHCache PluginEHCache integrates with the Hibernate Object/Relational Tool. It provides the in-process cache service for Hibernate. EHCache acts as a plugin for Hibernate 2.1 or later.
FeaturesEHCache is a pure Java, in-process cache with the following features:
- Fast
- Simple
- Acts as a pluggable cache for Hibernate 2.1.
- Small foot print. Both in terms of size and memory requirements.
- Minimal dependencies.
- Fully documented. See the online Documentation and the online JavaDoc.
Use With HibernateTo use EHCache download it from http://ehcache.sourceforge.net. JDK1.2 or higher is required. Install the ehcache-0.5.jar in your project classpath and do the following in Hibernate. For further documentation on EHCache see http://ehcache.sourceforge.net/documentation
Hibernate Configuration FileTo use EHCache with Hibernate, set the Hibernate property hibernate.cache.provider_class to net.sf.ehcache.hibernate.Provider. Commonly Hibernate properties are set using the hibernate.cfg.xml file. Add the following entry to the file:
<property name="hibernate.cache.provider_class">net.sf.ehcache.hibernate.Provider</property>
Hibernate Mapping FilesIn Hibernate, each domain object has a mapping file. So, for example to enable cache entries for the domain object com.somecompany.someproject.domain.Country there would be a mapping like:
<hibernate-mapping>
<class
name="com.somecompany.someproject.domain.Country"
table="ut_Countries"
dynamic-update="false"
dynamic-insert="false"
>
...
</hibernate-mapping>
To enable caching, add the following element.
<cache usage="read-write | read-only | |" />
I.e.
<cache usage="read-write" />
or
<cache usage="read-only" />
The cache element is renamed from jcs-cache. Hibernate Doclet generates jcs-cache elements. While deprecated these will also work with EHCache. Hibernate creates caches named after the fully qualified name of Domain Objects. So, for example to create a cache for com.somecompany.someproject.domain.Country create a cache configuration entry similar to the following in ehcache.xml.
<ehcache>
<cache name="com.somecompany.someproject.domain.Country"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
overflowToDisk="true"
/>
</ehcache>
|