英文:
Adding @Cachable to entities has no effect
问题
我想要缓存一些 JPA 实体以提高性能。所以我为我的 Spring Boot 应用程序设置了缓存,并将 javax.persistence.Cacheable 注解添加到一个实体类中。
@Entity
@Cacheable
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String firstName;
private String lastName;
}
这没有任何效果。如果我观察应用程序启动日志并保存和检索实体,我可以看到实体没有配置缓存,也没有从缓存中写入或读取任何内容。
然而,如果我将 @Cacheable
更改为 @org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
,缓存开始按预期工作。
由于我想坚持使用标准的 JPA 注解,所以问题是缺少了什么,以使 @Cacheable
起作用?
这是我的配置文件:
application.properties
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>
英文:
I want to cache some JPA entities to improve performance. So I set-up caching for my spring-boot application and added the javax.persistence.Cachable annotation to an entity.
@Entity
@Cacheable
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String firstName;
private String lastName;
}
This does nothing. If I watch the application startup log and saving and retreiving entities, I can see that no cache is configured for the entity and nothing is written or read from the cache.
However if I change @Cacheable
to @org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
the caching starts to work as expected.
Since I'd like to stick to standard JPA annotations, the question is what's missing to get @Cacheable
working?
This are my configuration files:
application.properties
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>
答案1
得分: 1
根据JPA 2.0规范,如果您想使用@Cacheable
注解有选择地缓存实体,您应该指定一个"共享缓存模式"。
spring.jpa.properties.javax.persistence.sharedCache.mode=ALL
英文:
According to the JPA 2.0 specification, if you want to selectively cache entities using the @Cacheable
annotation, you're supposed to specify a "share cache mode".
spring.jpa.properties.javax.persistence.sharedCache.mode=ALL
答案2
得分: 0
除了缓存模式和注解之外,还有一些属性/提示可以控制实体的缓存。
在JPA开发者指南中有一个关于缓存的部分:
https://cmobilecom.com/docs/jpa/latest/devguide/index.html
英文:
In addition to cache mode and annotations, there are some properties/hints which control caching of entities.
There is a Cache section in the JPA developer guide:
https://cmobilecom.com/docs/jpa/latest/devguide/index.html
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论