Configure Hibernate Cache
This section provides a complete guide on configuring Hibernate applications with NCache.
Note
This feature is also available in the NCache Community Edition.
Prerequisites to Configure Hibernate Cache
Before configuring Hibernate cache ensure that the following prerequisites are fulfilled:
- For using NCache Hibernate Maven packages for NCache Community Edition, replace the ncache-hibernate with ncache-community-hibernate in your pom.xml file.
- Make sure to include the following maven dependencies in your pom.xml file.
<dependency>
<groupId>com.alachisoft.ncache</groupId>
<artifactId>ncache-hibernate</artifactId>
<version>x.x.x</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>x.x.x</version>
</dependency>
Hibernate Cache Configuration
NCache has to be configured in the application's Hibernate configuration file as a second level cache. Following is a sample hibernate configuration file hibernate.cfg.xml
with Hibernate cache configuration.
<hibernate-configuration>
<session-factory>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.region.factory_class">com.alachisoft.ncache.NCacheRegionFactory</property>
<property name="ncache.application_id">myapp</property>
<property name="hibernate.cache.use_query_cache">true</property>
</session-factory>
</hibernate-configuration>
Properties added in the Hibernate’s configuration files are explained as under:
- Enable the use of a second level cache in your application by adding the following property in the Hibernate configuration section's
session-factory
tag:
<property name="hibernate.cache.use_second_level_cache">true</property>
- Configure NCache as Hibernate's second level cache provider. For Hibernate 3.6+, add the following property in Hibernate's configuration file under the
session-factory
tag:
<property name="hibernate.cache.region.factory_class">com.alachisoft.ncache.NCacheRegionFactory</property>
hibernate.cache.region.factory_class: This option lets you specify NCache's region cache factory class for Hibernate 3.6+. Hibernate will use this cache factory class to initialize and use NCache as a second level cache.
hibernate.cache.provider_class: This option lets you specify NCache as a second level cache provider. You need to mention NCache's cache provider class for Hibernate cache. This is how it knows how to call second-level cache.
- NCache provider for Hibernate identifies each application by an
application-id
that is later used to find the appropriate configuration for that application from the NCache configuration file for Hibernate ncache-hibernate.xml. Thisapplication-id
must be specified in Hibernate's configuration file. Add the following property in Hibernate's configurationsession-factory
tag for this purpose:
<property name="ncache.application_id">myapp</property>
- To use NCache as a query cache, enable the query cache by adding the following property:
<property name="hibernate.cache.use_query_cache">true</property>
See Also
Initialize Cache
Hibernate First Level Cache
Configure Cacheable Objects and Regions
Use Query Caching