Configure Hibernate Application
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>
<!-- Disable the second-level cache -->
<property name="hibernate.cache.use_second_level_cache">true</property>
<!-- com.alachisoft.ncache.integrations.hibernate.cache.NCacheRegionFactory-->
<property name="hibernate.cache.region.factory_class">JCacheRegionFactory</property>
<property name="hibernate.javax.cache.provider" >com.alachisoft.ncache.hibernate.jcache.HibernateNCacheCachingProvider</property>
<!-- <property name="hibernate.javax.cache.cache_manager">com.alachisoft.ncache.jcache.NCacheManager</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 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>
After configuring Hibernate application to use NCache, place NCache's Hibernate provider jar tg-hibernate.jar located at %NCHOME%/lib/integration/ in application's class path. In case of Hibernate version 3.5 and below, build source located at %NCHOME%/integration/hibernate-3.5 for required jar. Similarly add all jar files located at following directories to application's class path:
- %NCHOME%/lib
- %NCHOME%/lib/resources
See Also
Initialize Cache
Add/Update in Cache
Hibernate First Level Cache
Configure Cacheable Objects and Regions
Use Query Caching