Hibernate Tutorial
Core Hibernate
Hibernate Mapping
Hibernate Annotations
Hibernate with Spring Framework
Hibernate with Database
Hibernate Log4j
Inheritance Mapping
In Hibernate, caching plays a crucial role in improving application performance by reducing the number of direct database hits. Hibernate provides a two-level cache mechanism: the First Level Cache and the Second Level Cache.
Every Hibernate Session
has its built-in cache known as the First Level Cache. You don't need to enable it, as it's enabled by default.
No additional setup is required as it's on by default.
session.clear()
or for a particular object using session.evict(object)
.Unlike the First Level Cache, which is enabled by default, the Second Level Cache needs to be configured explicitly. It exists at the SessionFactory
level and is shared across sessions, making it more powerful.
Add Cache Provider Dependencies: You'd need to add cache provider dependencies to your project. EHCache, Infinispan, and Hazelcast are popular choices.
Example using EHCache (Maven dependency):
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-ehcache</artifactId> <version>Your_Hibernate_Version</version> </dependency>
Update Hibernate Configuration: Update your Hibernate configuration file (hibernate.cfg.xml
or persistence.xml
) to enable the second-level cache and specify the cache provider.
<property name="hibernate.cache.use_second_level_cache">true</property> <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
Enable Cache for Entities: Annotate your entity classes to enable caching.
@Entity @Cacheable @org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE) public class User { // ... }
Here, CacheConcurrencyStrategy.READ_WRITE
is a commonly used strategy. Others include NONSTRICT_READ_WRITE
, READ_ONLY
, etc.
(Optional) Configure Query Cache: Hibernate also supports caching of query result sets. To enable it:
<property name="hibernate.cache.use_query_cache">true</property>
Then, in your query, you can use:
query.setCacheable(true);
EHCache Configuration: For EHCache, you might need an ehcache.xml
configuration file to specify cache behavior, size, time-to-live, etc.
Leveraging Hibernate's caching mechanisms can significantly boost application performance by reducing direct database hits. While the First Level Cache provides automatic and straightforward benefits, the power of the Second Level Cache comes with added complexity and the responsibility of proper configuration and testing.
Configuring cache providers in Hibernate:
<!-- Hibernate configuration file --> <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
Cache concurrency strategies in Hibernate:
read-only
, nonstrict-read-write
, read-write
, and transactional
.<!-- Hibernate configuration file --> <property name="hibernate.cache.use_second_level_cache">true</property> <property name="hibernate.cache.use_query_cache">true</property> <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property> <property name="hibernate.cache.default_cache_concurrency_strategy">read-write</property>
Hibernate cache region configuration:
<!-- Hibernate configuration file --> <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property> <property name="hibernate.cache.region_prefix">myapp</property>
Cache eviction and expiration in Hibernate:
<!-- Hibernate configuration file --> <property name="hibernate.cache.use_second_level_cache">true</property> <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property> <property name="hibernate.cache.default_cache_concurrency_strategy">read-write</property> <property name="hibernate.cache.use_structured_entries">true</property> <property name="hibernate.cache.use_query_cache">true</property> <property name="hibernate.cache.use_minimal_puts">true</property> <property name="hibernate.cache.eviction_policy_class">org.hibernate.cache.internal.StandardQueryCache</property> <property name="hibernate.cache.use_query_cache">true</property> <property name="hibernate.cache.use_second_level_cache">true</property> <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property> <property name="hibernate.cache.use_query_cache">true</property> <property name="hibernate.cache.use_second_level_cache">true</property> <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>