Hibernate has no annotation called @EntityCache. The annotations that do the job are the JPA @Cacheable, which marks an entity as eligible for the second-level cache, and Hibernate's own @Cache (in org.hibernate.annotations), which sets the concurrency strategy and region. Result sets of queries are cached separately through the query cache.
This article explains how those pieces fit together, how to configure them with a JCache provider such as Ehcache 3, and when caching helps or hurts.
Searches for @EntityCache usually come from tutorials that describe entity caching with a made-up name, or from other frameworks. In Hibernate and JPA you use:
| Annotation | Package | Purpose |
|---|---|---|
@Cacheable | jakarta.persistence | Marks an entity (or collection owner) as cacheable in the shared cache |
@Cache | org.hibernate.annotations | Sets the concurrency strategy, region name and what to include for the entity or collection |
@QueryHints / setCacheable | Hibernate and JPA | Turns on caching for one query |
The second-level cache lives in the SessionFactory and is shared by all sessions. The first-level cache is the persistence context of one Session and is always on.
Add Hibernate's JCache integration and a provider. With Maven the artifacts are hibernate-jcache and org.ehcache:ehcache (use the jakarta classifier for Ehcache 3 with Hibernate 6 or later). Then set these properties, here in application.properties for Spring Boot:
In a plain hibernate.cfg.xml or persistence.xml you set the same keys without the Spring prefix. The old hibernate.cache.provider_class and EhCacheProvider from Hibernate 3 are gone; do not use them.
ENABLE_SELECTIVE means only entities explicitly marked with @Cacheable are cached, which is the safest default.
After the first session.get(Product.class, 1L) loads the row, later loads of the same id from another session are served from the cache without SQL. The cache stores the entity's state in a dehydrated form, not the object instance.
Concurrency strategies, from cheapest to strictest:
READ_ONLY: for data that never changes; updates throw an error.NONSTRICT_READ_WRITE: no locking, brief staleness possible after updates.READ_WRITE: soft locks keep readers consistent; the usual choice for mutable data.TRANSACTIONAL: needs a JTA-aware provider.Collections must be cached separately by putting @Cache on the collection field.
The query cache stores only identifiers and scalar results. For entity results, the entities themselves must be in the second-level cache, otherwise each id triggers its own SELECT and you create an N+1 problem.
In JPA code you can write .setHint("org.hibernate.cacheable", true) instead of setCacheable. Hibernate invalidates a cached query result when any table it reads is modified through Hibernate, so queries over frequently written tables gain little.
Turn on statistics and read the hit counters:
Set hibernate.generate_statistics=true first. A hit count of zero after repeated reads means the entity is not marked, the region factory is not configured, or each read happens inside the same session (served by the first-level cache).
Avoid the second-level cache for data that changes often, data written by other applications or by SQL outside Hibernate, and tables with many rows read once. Stale reads are the main risk. In a cluster, use a cache that replicates or invalidates across nodes, or keep the cache read-only.
No. Use @Cacheable from JPA and @Cache from org.hibernate.annotations.
Only if the second-level cache is enabled and the shared cache mode includes the entity. Without a configured region factory nothing is cached.
The first-level cache belongs to one session or EntityManager. The second-level cache belongs to the SessionFactory and is shared across sessions.
No. Cache a query only when it is run often, with the same parameters, on data that rarely changes.
There is no @EntityCache. Enable the second-level cache with a JCache provider, mark entities with @Cacheable and @Cache, and opt in individual queries with setCacheable(true). Measure hits with Hibernate statistics and cache only read-mostly data.