The Hibernate @Cache annotation (org.hibernate.annotations.Cache) turns on second-level caching for an entity or a collection and sets its concurrency strategy. The second-level cache is shared by every Session created from the same SessionFactory, so an entity loaded once by id can be served from memory to later sessions without another database query.
The annotation alone is not enough. You also need to enable the second-level cache and configure a cache provider, as shown below.
| First-level cache | Second-level cache | |
|---|---|---|
| Scope | One Session (persistence context) | The whole SessionFactory, across sessions |
| Enabled | Always, cannot be turned off | Opt in per entity or collection |
| Provider needed | No | Yes (Ehcache, Infinispan, Caffeine through JCache, and others) |
| Typical use | Identity guarantee and dirty checking | Reducing reads of rarely changing data |
Hibernate checks the first-level cache, then the second-level cache, and only then the database.
Add a JCache-compatible provider. With Hibernate 6 and Ehcache 3 the Maven dependencies look like this:
Then set the properties, in persistence.xml, hibernate.properties or, in Spring Boot, application.properties with the spring.jpa.properties. prefix:
Check your Hibernate version's documentation for the exact factory class names, because they changed between Hibernate 5 and 6. Turning on statistics lets you confirm hits and misses.
@Cacheable from jakarta.persistence is the standard JPA marker, and @Cache is the Hibernate-specific setting that chooses the strategy. Hibernate can cache an entity with @Cache alone, but using both makes intent clear and works with the JPA shared cache mode.
Now session.get(Country.class, 1L) in a new session is served from the cache, and Hibernate logs a second-level cache hit in the statistics.
The usage attribute decides how reads and writes are coordinated:
| Strategy | Behaviour | Use for |
|---|---|---|
READ_ONLY | Never updated, best performance | Reference data that never changes |
NONSTRICT_READ_WRITE | Updates invalidate entries after commit, small stale window possible | Data that rarely changes and tolerates brief staleness |
READ_WRITE | Soft locks prevent stale reads during updates | Most read-mostly entities |
TRANSACTIONAL | Full transactional cache, needs a JTA-capable provider | Special cases only |
READ_ONLY entities throw an error if you try to update them, so choose it only for truly immutable data.
Collections are cached separately from their owning entity, and only the identifiers of the elements are stored, so the element entity should be cached too:
Query results can be cached by setting hibernate.cache.use_query_cache=true and marking a query as cacheable, for example query.setCacheable(true). The query cache holds ids, is invalidated whenever a related table changes, and rarely pays off for frequently written tables.
No. @Cacheable marks an entity as eligible for the shared cache, while @Cache is Hibernate's annotation that sets the concurrency strategy.
No. You must enable the second-level cache and configure a provider, otherwise the annotation has no effect or startup fails.
Use READ_ONLY for immutable reference data and READ_WRITE for entities that change occasionally.
Hibernate's cache stores entity state by id inside the ORM, while Spring's @Cacheable caches the return values of arbitrary methods.
@Cache enables Hibernate's second-level cache for an entity or collection and sets its concurrency strategy. Add a JCache provider, enable the cache properties, pick READ_ONLY or READ_WRITE, and check statistics before relying on it. Use it for read-mostly data only.