I am trying to configure Ehcache (2.6.0) as Hibernate (3.6.4) second level cache.
I have set the following properties in the spring context file
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory</prop>
When I start the application, it fails with the exception:java.lang.ClassCastException: net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory cannot be cast to org.hibernate.cache.RegionFactory
Here is the stacktrace:
Caused by: org.hibernate.HibernateException: could not instantiate RegionFactory [net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory]
at org.hibernate.cfg.SettingsFactory.createRegionFactory(SettingsFactory.java:423)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:280)
at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2863)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2859)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1870)
Caused by: java.lang.ClassCastException: net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory cannot be cast to org.hibernate.cache.RegionFactory
at org.hibernate.cfg.SettingsFactory.createRegionFactory(SettingsFactory.java:409)
I have looked at the source and observed that SingletonEhCacheRegionFactory extends AbstractEhcacheRegionFactory and AbstractEhcacheRegionFactory implements RegionFactory
The code at SettingsFactory.java:409 is
return (RegionFactory) ReflectHelper.classForName( regionFactoryClassName )
.getConstructor( Properties.class )
.newInstance( properties );
Everything seems to be in place but I still get the exception. Any clues ?
Can you try by providing Provider Class:
org.hibernate.cache.EhCacheProvider
And also make sure that you have added required jar for ehCache.