In my application I use the hibernate-core-4.1.8 jar and would like to take an ehCache as 2nd level cache. The jar I am currently using is ehcache-core-2.5.0. I placed it in my WebContetn/WEB-INF/lib folder and put it in the classpath too.
My hibernate.cfg.xml looks like this:
<hibernate-configuration>
    <session-factory>   
    ....    
        <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
        <property name="hibernate.cache.use_second_level_cache">true</property>
        <property name="hibernate.cache.use_query_cache">true</property>
    </session-factory>
</hibernate-configuration>
the ehcache.xml which is placed in the JavaResources/src folder:
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"
        monitoring="autodetect" dynamicConfig="true">
    <defaultCache
                maxElementsInMemory="100000"
                eternal="false"
                timeToIdleSeconds="1000"
                timeToLiveSeconds="1000"
                overflowToDisk="false"
                />
</ehcache>
When running my application the class org.hibernate.cache.ehcache.EhCacheRegionFactory is not found:
java.lang.ClassNotFoundException: Could not load requested class : org.hibernate.cache.ehcache.EhCacheRegionFactory
    at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl$1.findClass(ClassLoaderServiceImpl.java:99)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:138)
    at org.hibernate.cfg.SettingsFactory.createRegionFactory(SettingsFactory.java:444)
    at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:275)
    at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2283)
    at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2279)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1748)
    at creator.controllers.MyFactory.<clinit>(MyFactory.java:18)
    at creator.controllers.SchoolDAO.getList(SchoolDAO.java:98)
    .....
                Ehcache as a plug-in second-level cache for Hibernate – Automatically cache common queries in memory to substantially lower latency. BigMemory for an in-memory store – Leverage off-heap physical memory to keep more of the data set close to your application and out of reach of Java garbage collection.
Ehcache is an open source, standards-based cache that boosts performance, offloads your database, and simplifies scalability. It's the most widely-used Java-based cache because it's robust, proven, full-featured, and integrates with other popular libraries and frameworks.
EhCache Library Terracotta Ehcache is a popular open-source Java cache that can be used as a Hibernate second-level cache.
The cache acts as a local copy of data retrieved from or stored to the system-of-record. This is often a traditional database, although it may be a specialized file system or some other reliable long-term storage. For the purposes of using Ehcache, the SOR is assumed to be a database.
you need to include hibernate-ehcache.jar by downloading it or is you're using maven put it in your dependencies like this 
<dependency>
<artifactId>hibernate-ehcache</artifactId>
<groupId>org.hibernate</groupId>
<version>4.0.0.CR6</version>
</dependency>
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With