The @Cacheable annotation in JPA (jakarta.persistence.Cacheable) marks an entity class as eligible for Hibernate's second-level cache. It is a hint to the persistence provider, not a method-level cache like Spring's @Cacheable. Whether it has any effect depends on the jakarta.persistence.sharedCache.mode setting and on a cache provider being configured.
The two annotations share a name and are often confused. This article explains what the JPA annotation does, how to turn the second-level cache on in Hibernate 6 with Spring Boot 3, and how it differs from Spring's caching abstraction.
@Cacheable is defined by the Jakarta Persistence specification and is applied to an entity class. It takes one attribute, value, which defaults to true.
Use @Cacheable(false) to exclude an entity from the shared cache. Whether the annotation is honoured is decided by the shared cache mode:
sharedCache.mode | Behaviour |
|---|---|
ALL | Every entity is cached |
NONE | No entity is cached |
ENABLE_SELECTIVE | Only entities annotated with @Cacheable(true) are cached |
DISABLE_SELECTIVE | All entities are cached except those with @Cacheable(false) |
UNSPECIFIED | Provider default, which in Hibernate behaves like ENABLE_SELECTIVE |
Persistence code sees up to three caches, and @Cacheable only concerns the second one.
EntityManager (Hibernate Session). It is always on, scoped to one transaction or session, and guarantees that find() for the same id returns the same instance inside that context.EntityManagerFactory. It stores the dehydrated state of entities, not the entity objects themselves. This is the cache @Cacheable controls.Hibernate 6 uses the JCache (JSR-107) integration for most providers. Ehcache 3 is a common choice. Add the dependencies:
Then configure Hibernate in application.yml:
@Cacheable only says that an entity may be cached. To control how it is cached, add Hibernate's own @Cache annotation, which sets the concurrency strategy and the region:
| Strategy | Use it for |
|---|---|
READ_ONLY | Reference data that never changes after insert |
NONSTRICT_READ_WRITE | Data that rarely changes and tolerates brief staleness |
READ_WRITE | General read-mostly data that must stay consistent |
TRANSACTIONAL | Full transactional caches, needs a JTA-aware provider |
Query results are not cached by @Cacheable on the entity. Enable hibernate.cache.use_query_cache and mark each query as cacheable:
With Spring Data JPA you can set the same hint on a repository method:
The query cache stores only the ids returned. Each entity is then resolved from the second-level cache, so a cached query over entities that are not cacheable still issues one select per row. Hibernate invalidates a query cache region whenever a table it reads from is modified, which makes it a poor fit for write-heavy tables.
| Aspect | jakarta.persistence.Cacheable | org.springframework.cache.annotation.Cacheable |
|---|---|---|
| Applied to | Entity classes | Methods (or classes) |
| What is cached | Entity state, keyed by id | Any method return value |
| Managed by | Hibernate and the cache provider | Spring cache abstraction (CacheManager) |
| Invalidation | Automatic on entity updates through JPA | You manage it with @CacheEvict or @CachePut |
| Needs configuration | Shared cache mode plus a provider | @EnableCaching plus a cache manager |
Bulk JPQL or native updates bypass the entity lifecycle, so Hibernate cannot always evict the affected entries. After bulk statements, evict explicitly through entityManagerFactory.getCache().evictAll() or evict(Country.class).
No. The first-level cache is the persistence context and is always active. @Cacheable affects only the shared second-level cache.
Yes. Without a configured provider and use_second_level_cache set to true, the annotation is ignored and every find() outside the persistence context reads from the database.
Yes, if rows change outside Hibernate, for example through another application, a native SQL update or a database trigger. Use short time-to-live values, or cache only data that changes rarely.
No. Cache read-mostly entities with high hit rates, such as countries or product categories. Write-heavy entities spend more time invalidating the cache than they gain from it.
JPA's @Cacheable is an entity-level opt-in for the second-level cache, interpreted according to the shared cache mode. It needs a provider such as Ehcache through JCache, and Hibernate's @Cache to choose a concurrency strategy. It is unrelated to Spring's method-level @Cacheable, which you use to cache arbitrary return values.