Clean • Professional
Hibernate Caching is a mechanism used to reduce database calls by storing frequently accessed data in memory.
Instead of querying the database repeatedly for the same data, Hibernate retrieves it from the cache, improving performance.

In simple words:
Hibernate cache keeps data in memory so the database is not queried again and again for the same data.
Hibernate supports three levels of caching:

Hibernate always checks caches in this order.
The First-Level Cache (L1 Cache), also known as the Session Cache, is the default caching mechanism in Hibernate.
It is automatically enabled and is tightly bound to a single Hibernate Session. This cache cannot be disabled and works behind the scenes to improve performance.
Whenever an entity is fetched from the database, Hibernate stores it in the first-level cache. If the same entity is requested again within the same session, Hibernate returns it directly from memory instead of executing another SQL query.
How First-Level Cache Works
Example
Sessionsession= sessionFactory.openSession();
Useru1= session.get(User.class,1L);// Database hit
Useru2= session.get(User.class,1L);// Retrieved from L1 cache
session.close();
Key Points
The Second-Level Cache (L2 Cache) is an optional caching mechanism in Hibernate that stores entity data across multiple sessions.
Unlike the First-Level Cache, which is limited to a single Session, the L2 cache works at the SessionFactory level and is shared among all sessions. This allows Hibernate to reuse cached data even when different sessions request the same entity.
How Second-Level Cache Works
Entity Configuration Example
@Entity
@Cacheable
@org.hibernate.annotations.Cache(
usage = CacheConcurrencyStrategy.READ_WRITE
)
public class Product {
}
Popular Second-Level Cache Providers
Hibernate does not provide a cache itself. Instead, it integrates with external cache providers such as:
When Should You Use Second-Level Cache?
Use Second-Level Cache when:
The Query Cache is a caching mechanism in Hibernate that stores the results of queries rather than the entity data itself.
It depends on the Second-Level Cache (L2 Cache) to fetch actual entity data and is disabled by default.
Using the query cache can improve performance for repeated queries by avoiding database hits for the same query.
How Query Cache Works
This reduces database queries for frequently executed read-only queries.
Example Usage
Queryquery= session.createQuery("from Product");
query.setCacheable(true);// Enable query caching
List<Product> products = query.list();
Important Notes
Hibernate provides three levels of caching to improve performance and reduce database load: First-Level Cache (L1), Second-Level Cache (L2), and Query Cache.
| Feature | First-Level Cache (L1) | Second-Level Cache (L2) | Query Cache |
|---|---|---|---|
| Scope | Hibernate Session (one session) | SessionFactory (shared across sessions) | SessionFactory (relies on L2 cache) |
| Enabled By Default? | Yes, cannot be disabled | No, optional | No, disabled by default |
| Data Stored | Entities loaded in the session | Entities shared across sessions | Query result IDs (entities fetched from L2) |
| Cache Lifetime | Tied to session lifetime | Tied to SessionFactory lifetime | Depends on L2 cache and query configuration |
| Shared Across Sessions? | No | Yes | Yes, indirectly via L2 cache |
| Use Case | Avoid duplicate database calls within the same session | Reduce DB hits for frequently read, rarely updated entities | Speed up repeated query execution |
| Example | session.get(User.class, 1L) | @Cacheable on entity like Product | query.setCacheable(true) |

Hibernate offers different caching strategies to control how cached data is read, updated, and synchronized. Choosing the right strategy ensures data consistency, performance, and safety in concurrent applications.
READ_WRITE because synchronization is less strict.Summary Table:
| Strategy | Use Case |
|---|---|
| READ_ONLY | Immutable data (fastest) |
| READ_WRITE | Read-heavy, occasional updates |
| NONSTRICT_READ_WRITE | Allows stale data |
| TRANSACTIONAL | Full transactional consistency (JTA) |
Cache eviction is the process of removing data from the cache to keep it consistent and free up memory. This ensures that stale or outdated data does not affect your application.
You can explicitly remove cached entities or entire cache regions using Hibernate APIs:
sessionFactory.getCache().evictEntityData(Product.class);
Hibernate can automatically evict cached data in certain situations:
Use Cache When:
Avoid Cache When:
| Feature | Hibernate Cache | Database Cache |
|---|---|---|
| Level | Operates at the ORM (Hibernate) level | Managed by the database engine |
| Awareness | Entity-aware – understands entities and relationships | Row/page-based – works with raw database rows or pages |
| Scope | Application-specific – used within the application/session | Shared across all applications accessing the database |
Hibernate caching improves performance by reducing repeated database calls and lowering database load, especially in read-heavy applications. The first-level cache works automatically within a session, while the second-level cache shares data across sessions for frequently read, rarely updated data. The query cache can further optimize performance but should be used carefully.
Caching should be applied selectively—using it for stable, read-heavy data boosts performance and scalability, but caching frequently changing data can cause consistency issues.