- First-level caching: Cache entities
- Second-level cache: Cache Entities
- 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).
- 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