Hibernate 6是否与Sping Boot 3的Ehcache 3不兼容?

ocebsuys  于 6个月前  发布在  其他
关注(0)|答案(1)|浏览(104)

在Sping Boot 3应用程序中,我有以下依赖项:

<dependency>
    <groupId>org.hibernate.orm</groupId>
    <artifactId>hibernate-jcache</artifactId>
</dependency>

<dependency>
    <groupId>org.ehcache</groupId>
    <artifactId>ehcache</artifactId>
    <version>${ehcache3.version}</version>
</dependency>

字符串
然后,在application.yml中设置以下属性:

spring.jpa.properties:
  -hibernate.generate_statistics: true
  -hibernate.cache.use_second_level_cache: true
  -hibernate.cache.region.factory_class: "jcache"
  -hibernate.javax.cache.provider: "org.ehcache.jsr107.EhcacheCachingProvider"


最后,@EnableCaching被应用到一个config类。但是上面的所有配置似乎都不起作用,因为Hibernate仍然会为以下实体的每个请求进行一次数据库之旅:

@Entity
@Access(AccessType.FIELD)
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Table(name = "email_status")
public class EmailStatus implements Serializable {

  //...
}


查询和统计输出始终相同:

select
        e1_0.email_status_id,
        e1_0.description 
    from
        email_status e1_0 
    where
        e1_0.description=?
21:45:12 TRACE - [nio-8087-exec-2] o.h.o.j.bind                                      :  binding parameter [1] as [VARCHAR] - [PENDING]

...

21:45:12 INFO  - [nio-8087-exec-2] o.h.e.i.StatisticalLoggingSessionEventListener    :  Session Metrics {
    12566 nanoseconds spent acquiring 1 JDBC connections;
    0 nanoseconds spent releasing 0 JDBC connections;
    42664 nanoseconds spent preparing 1 JDBC statements;
    73055 nanoseconds spent executing 1 JDBC statements;
    0 nanoseconds spent executing 0 JDBC batches;
    301863 nanoseconds spent performing 1 L2C puts;
    0 nanoseconds spent performing 0 L2C hits;
    0 nanoseconds spent performing 0 L2C misses;
    87958 nanoseconds spent executing 1 flushes (flushing a total of 1 entities and 0 collections);
    0 nanoseconds spent executing 0 partial-flushes (flushing a total of 0 entities and 0 collections)
}


如果我不使用任何特定于Hibernate的设置,而只是让spring-boot-starter-cache完成它的工作,那么用org.springframework.cache.annotation.Cacheable缓存注解实体就可以了--也许性能不是那么好,但至少是可以的。
这里的Hibernate文档在信息框中告诉你可以在on the JPC website中找到兼容的JCache实现。指向支持的Ehcache实现的链接会把你带到这个有10年历史的Ehcache 2.x version。这不会是严重的吧?
为什么我找不到任何关于如何为Sping Boot 3应用程序配置Hibernate 6和Ehcache 3的实际文档?有没有可能,这是一个不起作用的组合?我找到的都是过时的文档或没有任何上下文的fiddeling示例。然而,在花了几个小时试图让事情工作后,我放弃了。要么这根本不可能,要么我太笨了。
如果有人知道如何在Sping Boot 3中为Hibernate 6设置一个合适的Ehcache 3,那就太好了。非常感谢。

x4shl7ld

x4shl7ld1#

尝试使用这个而不是org.springframework.cache.annotation.Cacheable

@jakarta.persistence.Cacheable

字符串
这里是你需要添加的最小配置。除了spring-data-jpa,需要的依赖关系很少。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>org.ehcache</groupId>
    <artifactId>ehcache</artifactId>
    <version>3.10.8</version>
</dependency>
<dependency>
    <groupId>org.hibernate.orm</groupId>
    <artifactId>hibernate-jcache</artifactId>
    <version>6.3.1.Final</version>
</dependency>

的数据
此外,您需要将这些配置添加到application.properties以启用二级缓存。

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


在实体类级别上,使用此选项启用缓存

@jakarta.persistence.Cacheable
@org.hibernate.annotations.Cache
public class EmailStatus {
}

相关问题