NOTES: Hibernate Query cache

Source: Internet
Author: User

Hibernate's first level cache and level two cache are caches of entities, and he does not cache normal attributes, and if you want to cache normal familiarity, consider using query caching.

For the query cache, the key that he caches is the HQL or SQL statement used by the query, the query cache not only requires the same statement, but also requires that the parameters must be consistent, the query cache is closed by default, in order to open the query cache, the Hibernate.cfg.xml configuration file needs to be modified. Add the following configuration:

<property name= "Hibernate.cache.use_query_cache" >true</property>

To use the query cache, you must also use the Set Cacheable method to turn the cache on when the query is called ,as shown in the example code:

??

public static void Main (string[] args) {

Session cursession = Hibernateutils.opensession ();

try {

??

First query, Cache enabled

List List = Cursession.createquery ("Select TRM from Transitrecordmodel TRM"). Setcacheable (True). List ();

System.out.println ("Query size=" + list.size ());????????????????

Second query, cache enabled, read from cache, do not initiate database query operation

List = Cursession.createquery ("Select TRM from Transitrecordmodel TRM"). Setcacheable (True). List ();

System.out.println ("Query size=" + list.size ());

} finally {

if (cursession! = null) {

Cursession.close ();

}

}

}

??

interaction between the query cache and the level two cache There are several types of settings that are set by the set CacheMode method:

    1. Cachemode.normal: Normal mode (default), session reads and writes data to the level two cache.
    2. Cachemode.ignore: Ignore mode, session does not read to level two cache and does not write data.
    3. CacheMode.GET:Session the level two cache is read-only and does not write.
    4. CacheMode.PUT:Session write-only to the level two cache.
    5. Cachemode.refresh: Refresh mode, session does not read data from the level two cache, but it writes data read from the database. The difference between the and put is that refresh ignores the Hibernate.cache.use_minimal_puts property in the configuration file and forces all data in the level two cache to be refreshed.

??

??

NOTES: Hibernate Query cache

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.