hibernate4.1.2使用ehcache2.4.3实现二级缓存时报错

本文介绍了一种在Hibernate中配置ehCache作为二级缓存的方法。通过详细展示hibernate.cfg.xml及ehcache.xml配置文件的例子,解决了由于缺少hibernate-ehcache.jar导致的ClassNotFoundException异常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我的异常已解决,就借鉴一下人家的异常吧

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>

ehcache.xml配置文件

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

异常:

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


解决办法:

 

you need to include hibernate-ehcache.jar by downloading it or is you're using maven put it in your dependencies like thi

 

 

转载于:https://www.cnblogs.com/yeahwell/archive/2013/01/22/5226064.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值