Try Playground
Show / Hide Table of Contents

Use Query Caching in Hibernate Cache

Query caching uses the L2 cache to store queries so that whenever the same query needs to be executed again, it can be fetched from the cache. Please note that any object retrieved as a result of the query is cached to their respective regions. Therefore, mark objects as cacheable for efficient use of query caching.

Important

The query and the result set primary keys are stored in a default query region called "org.hibernate.cache.StandardQueryCache", which is a fully qualified name of Hibernate's StandardQueryCache class. However, this is only supported for Hibernate-5.2 and below.

Note

Since not every query's results remain the same for a period of time, use query caching with queries whose results are less likely to change frequently.

Enable Query Caching in Hibernate Cache

To enable query caching, in Hibernate cache add the following tag to your configuration in hibernate.cfg.xml.

<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>

Enabling the query cache in Hibernate cache does not cache each query by default. Instead queries that require to be cached are to be set as cacheable in the code. To set a query as cacheable, call the setCacheable(true) function of the query while creating a query. The code below is an example showing a cacheable query in Hibernate cache:

var session = sessionFactory.openSession();
ArrayList<Products> productList = (ArrayList<Products>) session.createQuery("from Products p", Products.class).setCacheable(true).list();
for (var product : productList) {
    System.out.println("Retrieved Product: " + product);
}
session.close();

See Also

Initialize Cache
Hibernate First Level Cache
Configure Cacheable Objects and Regions
Configure Hibernate Application

In This Article
  • Enable Query Caching in Hibernate Cache
  • See Also

Contact Us

PHONE

+1 (214) 764-6933   (US)

+44 20 7993 8327   (UK)

 
EMAIL

sales@alachisoft.com

support@alachisoft.com

NCache
  • NCache Enterprise
  • NCache Professional
  • Edition Comparison
  • NCache Architecture
  • Benchmarks
Download
Pricing
Try Playground

Deployments
  • Cloud (SaaS & Software)
  • On-Premises
  • Kubernetes
  • Docker
Technical Use Cases
  • ASP.NET Sessions
  • ASP.NET Core Sessions
  • Pub/Sub Messaging
  • Real-Time ASP.NET SignalR
  • Internet of Things (IoT)
  • NoSQL Database
  • Stream Processing
  • Microservices
Resources
  • Magazine Articles
  • Third-Party Articles
  • Articles
  • Videos
  • Whitepapers
  • Shows
  • Talks
  • Blogs
  • Docs
Customer Case Studies
  • Testimonials
  • Customers
Support
  • Schedule a Demo
  • Forum (Google Groups)
  • Tips
Company
  • Leadership
  • Partners
  • News
  • Events
  • Careers
Contact Us

  • EnglishChinese (Simplified)FrenchGermanItalianJapaneseKoreanPortugueseSpanish

  • Contact Us
  •  
  • Sitemap
  •  
  • Terms of Use
  •  
  • Privacy Policy
© Copyright Alachisoft 2002 - 2025. All rights reserved. NCache is a registered trademark of Diyatech Corp.
Back to top