Hibernate level two cache troubleshooting points

Source: Internet
Author: User

    1. First-level caching: Cache entities
    2. Second-level cache: Cache Entities
    3. The hibernate query cache caches the partial attribute result set and entity ID of the entity being queried (note that this is not an entity).
    4. Hibernate query cache: Works on list. However, hibernate query caching does not work for iterator, only for list.

Iterator does not consider caching, it must execute, and only the collection of entity IDs is queried.

Iterator iter = Session.createquery ("from Gouser"). Iterate ();  for (; Iter.hasnext ();) {    = (gouser) Iter.next ();    System.out.println (U.getname ());}

Description: Iterator This method gets the ID collection of the entity, which is re-issued to the database when querying the entity's specific properties.

If a level two cache is turned on, the cycle through the two level cache will save the entity.

Turn off level Two cache, turn on query caching

Session session =sf.opensession (); Session.begintransaction (); List<User> list = (list<user>) session.createquery ("from User"). Setcacheable (true). List (); for(User u:list) {System.out.println (U.getname ());} Session.gettransaction (). commit (); Session.close (); System.out.println ("-----------------Split Line------------------"); Session Session2=sf.opensession (); Session2.begintransaction (); List<User> List2 = (list<user>) session2.createquery ("from User"). Setcacheable (true). List (); for(User u:list2) {System.out.println (U.getname ());} Session2.gettransaction (). commit (); Session2.close ();

Execution Result:

Description

Turn off level Two cache, open the query cache, the first list query, the property information of all entities query out, only one statement executed, and then only save the list of entity ID, so the second query, the ID in the query cache to the database to fetch the entity data.

When the second-level cache and the query cache are turned on

Code as in the example above

Execution Result:

Description

When both the secondary and query caches are turned on, because the query statement is the same, the corresponding ID collection is looked up from the query cache, and then the corresponding entity is looked up from the level two cache based on the ID, so only the database operation is performed once.

Hibernate level two cache troubleshooting points

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.