java.lang.ClassCastException:net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory无法强制转换为org.hibernate.cache.RegionFactory

时间:2013-02-06 09:28:37

标签: java spring hibernate ehcache classcastexception

我正在尝试将Ehcache(2.6.0)配置为Hibernate(3.6.4)二级缓存。 我在spring上下文文件中设置了以下属性

<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>

当我启动应用程序时,它失败并出现例外:java.lang.ClassCastException: net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory cannot be cast to org.hibernate.cache.RegionFactory

这是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)

我查看了来源并观察了SingletonEhCacheRegionFactory extends AbstractEhcacheRegionFactoryAbstractEhcacheRegionFactory implements RegionFactory

SettingsFactory.java:409上的代码是

            return (RegionFactory) ReflectHelper.classForName( regionFactoryClassName )
                    .getConstructor( Properties.class )
                    .newInstance( properties );

似乎一切都已到位,但我仍然得到例外。有线索吗?

1 个答案:

答案 0 :(得分:1)

您可以尝试提供提供商类:

org.hibernate.cache.EhCacheProvider

并确保为ehCache添加了所​​需的jar。