I'm using spring data jpa in my spring boot application and I need to enable hibernate second level cache for caching some entities. I use Redis as cache server for my app and so I need to use Redisson with its spring boot integration. Here is my some code examples for enable second level cache:
@Entity
@Cacheable
@Cache(region = "baseInfoCache", usage = CacheConcurrencyStrategy.READ_WRITE)
public class BaseInfo {
}
application.yml:
spring:
jpa:
properties:
hibernate:
cache:
use_query_cache: true
use_second_level_cache: true
use_structured_entries: true
region_prefix: dd_hibernate
region:
factory_class: org.redisson.hibernate.RedissonRegionFactory
redisson:
fallback: true
config: redisson/redisson-dev.yaml
redisson-dev.yml:
singleServerConfig:
address: "redis://localhost:6379"
My BaseInfoRepository extends jpa standard CrudRepository. After calling findAll() method for first time, I can see the cache in redis, so putting data into cache is working without problem. But, I can see hibernate select query logs next times when calling findAll() method. It means hibernate does not load data from my cache. When I override the findAll() method in my repository and put @QueryHints on it, hibernate starts to reading from cache! But I don't want to override native spring data methods. In the other words, I want to use hibernate second level cache in spring data default methods without using @QueryHints. Is that possible?