Spring Boot 1.5.1-休眠二级缓存 [英] spring boot 1.5.1- hibernate second level cache

查看:65
本文介绍了Spring Boot 1.5.1-休眠二级缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在spring-boot 1.5.1中实现休眠二级缓存。遇到以下错误

I am trying to implement hibernate second level cache in spring-boot 1.5.1 . Facing the below error

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 using the hibernate.cache.region.factory_class setting and make sure the second level cache provider (hibernate-infinispan, e.g.) is available on the classpath.

会话配置

hibernateProperties.put("hibernate.cache.use_second_level_cache", true);
hibernateProperties.put("hibernate.cache.provider_class", "org.hibernate.cache.EhCacheProvider");
hibernateProperties.put("hibernate.cache.region.factory_class", "org.hibernate.cache.ehcache.EhCacheRegionFactory");

JPA POJO

@Entity
@Cacheable
@Cache(usage= CacheConcurrencyStrategy.READ_ONLY, region="messageCache")
@Table(name="AWARD")
public class Award {

ehcache.xml

ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
    <defaultCache eternal="true" maxElementsInMemory="1000" overflowToDisk="false" />
    <cache name="messageCache" maxElementsInMemory="100" eternal="true" overflowToDisk="false" />
</ehcache>


推荐答案

我刚刚使用Spring Boot 1.5.1和使用 hibernate.properties 中的以下设置进行Hibernate 5.2.8(甚至在Java配置中创建实体管理器bean时指定属性也可以)

I just tested this using Spring Boot 1.5.1 and Hibernate 5.2.8 using the following settings in hibernate.properties (even specifying the properties when creating the entity manager bean in java config works too)

hibernate.cache.use_second_level_cache=true   
hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory

我按如下所示设置一个简单实体:

I setup a simple entity as follows:

@Entity
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY, region = "simpleCache")
public class Simple {
}

并按如下所示指定我的 ehcache.xml

And specified my ehcache.xml as follows:

<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
  <defaultCache eternal="true" maxElementsInMemory="1000" overflowToDisk="false"/>
  <cache name="simpleCache" maxElementsInMemory="100" eternal="true" overflowToDisk="false" />
</ehcache>

pom.xml内容:

The pom.xml things:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.hibernate.stackoverflow</groupId>
    <artifactId>stackoverflow</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>stackoverflow</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.2.8.Final</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-ehcache</artifactId>
            <version>5.2.8.Final</version>
        </dependency>

        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache-core</artifactId>
            <version>2.4.3</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

弹簧刚好启动:

INFO 8674 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.2.8.Final}
INFO 8674 --- [           main] org.hibernate.cfg.Environment            : HHH000205: Loaded properties from resource hibernate.properties: {hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory, hibernate.cache.use_second_level_cache=true, hibernate.bytecode.use_reflection_optimizer=false}
INFO 8674 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
INFO 8674 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
WARN 8674 --- [           main] c.e.i.s.EhcacheAccessStrategyFactoryImpl : HHH020007: read-only cache configured for mutable entity [simpleCache]

这篇关于Spring Boot 1.5.1-休眠二级缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆