Configure Hibernate Application
Note
For usage of Maven packages for Professional Edition, you just need to change the <artifactId>
and add the word professional in it.
Pre-Requisites
Make sure to include the following maven dependencies in your pom.xml
file.
<dependency>
<groupId>com.alachisoft.ncache</groupId>
<artifactId>ncache-hibernate</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.4.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jcache</artifactId>
<version>5.4.4.Final</version>
</dependency>
Hibernate Application Configuration
NCache has to be configured in application's Hibernate configuration file as second level cache. Following is a sample hibernate's 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 hibernate’s configuration file are explained as under:
- Enable use of second level cache in your application by adding the following property in 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 Hibernte 3.6 and above add following property in Hibernate's configuration file under 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 and above. Hibernate will use this cache factory class to initialize and use NCache as second level cache.
For Hibernate versions 3.5 and below instead of hibernate.cache.region.factory_class add following property in Hibernate's configuration file under 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 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 appropriate configuration for that application from NCache configuration file for Hibernate "NCacheHibernate.xml". This application id must be specified in hibernate's configuration file. Add following property in hibernate's configuration session-factory tag for this purpose:
<property name="ncache.application_id">myapp</property>
- To use NCache as query cache, enable query cache by adding 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