Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将@cacable添加到实体没有效果_Java_Jpa_Caching_Second Level Cache - Fatal编程技术网

Java 将@cacable添加到实体没有效果

Java 将@cacable添加到实体没有效果,java,jpa,caching,second-level-cache,Java,Jpa,Caching,Second Level Cache,我想缓存一些JPA实体以提高性能。因此,我为我的spring boot应用程序设置了缓存,并将注释添加到实体中 @Entity @Cacheable public class Customer { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String firstName; private String lastName; } 这没用。如果

我想缓存一些JPA实体以提高性能。因此,我为我的spring boot应用程序设置了缓存,并将注释添加到实体中

@Entity
@Cacheable
public class Customer {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String firstName;
    private String lastName;

}
这没用。如果我查看应用程序启动日志以及保存和检索实体,我可以看到没有为实体配置缓存,也没有从缓存中写入或读取任何内容

但是,如果我将
@Cacheable
更改为
@org.hibernate.annotations.Cache(用法=cacheconcurrentystrategy.READ\u WRITE)
缓存将按预期工作

既然我想坚持使用标准的JPA注释,那么问题是让
@Cacheable
工作缺少什么

这是我的配置文件:

应用程序属性

spring.jpa.properties.hibernate.cache.use_second_level_cache=true
spring.jpa.properties.hibernate.cache.region.factory_class=jcache
spring.jpa.properties.hibernate.javax.cache.provider=org.ehcache.jsr107.EhcacheCachingProvider

logging.level.org.hibernate.cache=DEBUG
logging.level.org.ehcache=DEBUG
logging.level.org.hibernate.event.internal.DefaultLoadEventListener=TRACE
pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.3.RELEASE</version>
    </parent>
    <groupId>com.example</groupId>
    <artifactId>accessing-data-jpa</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>accessing-data-jpa</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
    </properties>

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

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-jcache</artifactId>
        </dependency>
        <dependency>
            <groupId>org.ehcache</groupId>
            <artifactId>ehcache</artifactId>
        </dependency>
        <dependency>
            <groupId>org.ehcache</groupId>
            <artifactId>ehcache-transactions</artifactId>
        </dependency>
    </dependencies>

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

</project>

4.0.0
org.springframework.boot
spring启动程序父级
2.3.3.2发布
com.example
访问数据jpa
0.0.1-快照
访问数据jpa
SpringBoot的演示项目
11
org.springframework.boot
spring引导启动器数据jpa
com.h2数据库
氢
运行时
org.springframework.boot
弹簧起动试验
测试
org.junit.vintage
朱尼特老式发动机
org.hibernate
冬眠
org.ehcache
ehcache
org.ehcache
ehcache事务
org.springframework.boot
springbootmaven插件

根据JPA2.0规范,如果您想使用
@Cacheable
注释选择性地缓存实体,您应该指定“共享缓存模式”


除了缓存模式和注释外,还有一些属性/提示控制实体的缓存

JPA开发者指南中有一个缓存部分:

这确实有帮助。然而,我选择了ENABLE_SELECTIVE而不是ALL。“全部”意味着缓存所有内容,而不考虑任何注释。
spring.jpa.properties.javax.persistence.sharedCache.mode=ALL