Relationship between hibernate second-level cache and query Cache

Source: Internet
Author: User

Relationship between hibernate second-level cache and query Cache

Let's take a look at what kind of hibernate operations will store the data in the first-level cache. When the following methods are called, the data will be saved to the first-level cache.

Level 1 Cache

Save, update, saveOrupdate, load, list, iterate, lock, for example:

 

User userOne = new User (); user. setName ("name"); // put it into the first-level cache s. save (user); // query User userTwo = (User) s. get (User. class, student. getId ());

Get, load, list

 

 

User user=(User) s.get(User.class, 45);                      String hql="from User where id=45";  User user2=(User) s.createQuery(hql).uniqueResult();    System.out.println(stu2.getName());  

 

 

Level 2 Cache and query cache Level 2 cache are SessionFactory-level global cache, which provides cache for each class (or set. Any query method that calls the second-level cache will benefit from this, such as load, list, iterate, and other methods (note that get and find do not use the cache to directly access the database ).
Opening conditions:
1) hibernate. cache. provider_class = org. hibernate. cache. EhCacheProvider (ehcache is specified here)
2) hbm. xml configuration
3) ehcache. xml defines the cache of this class
In short, the second-level cache is used to save class objects and fill in all its attribute values.

The query cache stores the SQL statements and result sets queried by the query method (such as list and iterate), which has nothing to do with the load object method. That is to say, the query cache ignores the load method.
In addition, this result set is only a list of object IDs, and other object attribute values are not saved. For example, when a list is executed, the list of object IDS is obtained based on the SQL statement in the query cache, and then the object is loaded (id.
Opening conditions:
1) hibernate. cache. use_query_cache = true
2) Set setCacheable (true) when using Query or Criteria)
In short, the query cache is used to save the list of object IDs obtained after SQL Execution. The object id list can be obtained directly from the query cache when the same SQL statement is executed next time.

The query cache only stores the Object id and does not store the real object. The query cache uses the hql statement + parameter + paging as the key value.

 

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.