Spring使用ehcache进行休眠和缓存

时间:2014-04-11 17:44:05

标签: spring hibernate ehcache

我正在使用spring 3.2和hibernate 4.2.8并尝试使用ehcache但是当我使用@Cache(org.hibernate.annotations.Cache)注释实体类时,我得到以下异常

Caused by: org.hibernate.cache.NoCacheRegionFactoryAvailableException: Second-level cache is used in the application, but property hibernate.cache.region.factory_class is not given, please either disable second level cache or set correct region factory class name to property hibernate.cache.region.factory_class (and make sure the second level cache provider, hibernate-infinispan, for example, is available in the classpath).
    at org.hibernate.cache.internal.NoCachingRegionFactory.buildEntityRegion(NoCachingRegionFactory.java:69)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:351)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1797)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1868)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:247)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:373)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:358)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1547)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1485)
    ... 42 more

我的配置如下所示

<bean id="sessionFactoryAdmin"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="adminDataSource" />
        <property name="packagesToScan" value="com.digilegal.services.ahc.model.user" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <!-- <prop key="hibernate.hbm2ddl.auto">update</prop> -->
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
                <prop key="hibernate.cache.provider_configuration">/WEB-INF/ehcache-entity.xml</prop>
                <prop key="hibernate.cache.use_second_level_cache">true</prop>
                <prop key="hibernate.cache.use_query_cache">true</prop>
            </props>
        </property>
    </bean>

我已尝试过互联网上所有可能的链接,但无法解决问题

这是我的/WEB-INF/ehcache-entity.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <ehcache>
    <cache name="com.digilegal.services.ahc.model.user.UserNotifications"
        maxElementsInMemory="500"
        eternal="false"
        overflowToDisk="false"
        timeToIdleSeconds="69"
        timeToLiveSeconds="65" />


    <cache name="org.hibernate.cache.StandardQueryCache"
        maxEntriesLocalHeap="15" eternal="false" timeToLiveSeconds="60"
        overflowToDisk="true" />
</ehcache>

由于 Nirav

1 个答案:

答案 0 :(得分:11)

我最近遇到了这个错误,在我的情况下,错误发生在使用不同会话工厂配置的测试中,而没有指定hibernate.cache.region.factory_class

因此,@Cache注释的存在导致测试以完全相同的错误中止。 在我的情况下,我不想为特定的会话工厂缓存,所以我补充说:

<prop key="hibernate.cache.use_second_level_cache">false</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>

查看您的配置,看看您是否定义了多个会话工厂,而有些工作室不提供hibernate.cache.region.factory_class