Configure Hibernate Application
Prerequisites
- For using NCache Hibernate Maven packages for NCache Professional, replace the ncache-hibernate with ncache-professional-hibernate in your pom.xml.
- 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>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jcache</artifactId>
<version>x.x.x</version>
</dependency>
Hibernate Application 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 caching configuration.
<hibernate-configuration>
<session-factory>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.region.factory_class">JCacheRegionFactory</property>
<property name="hibernate.javax.cache.provider" >com.alachisoft.ncache.hibernate.jcache.HibernateNCacheCachingProvider</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">JCacheRegionFactory</property>
hibernate.cache.region.facory_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.
For Hibernate versions 3.5 and below, instead of hibernate.cache.region.factory_class, add the following property in Hibernate's configuration file under the session-factory tag:
<property name="hibernate.javax.cache.provider" >com.alachisoft.ncache.hibernate.jcache.HibernateNCacheCachingProvider</property>
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. This is how Hibernate 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’. This application-id must be specified in hibernate's configuration file. Add the following property in Hibernate's configuration session-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
Add/Update in Cache
Hibernate First Level Cache
Configure Cacheable Objects and Regions
Use Query Caching