explain Hibernate query cache

Esca Tran

I read the article in http://www.javalobby.org/java/forums/t48846.html which explain Hibernate Query Cache but I couldn't completely understand how it works.

As the article explain:

Query query = session.createQuery("from Person as p where p.parent.id=? and p.firstName=?");
query.setInt(0, Integer.valueOf(1));
query.setString(1, "Joey");
query.setCacheable(true);
List l = query.list();

The query cache works something like this:

*----------------------------------------------------------------------------------------*
|                                    Query Cache                                         |
|----------------------------------------------------------------------------------------|
| ["from Person as p where p.parent.id=? and p.firstName=?", [ 1 , "Joey"] ] -> [  2 ] ] |
*----------------------------------------------------------------------------------------*

The combination of the query and the values provided as parameters to that query is used as a key, and the value is the list of identifiers for that query.

Question 1: What are identifiers in this? Are they the entities' ID in query result?

Question 2: Hibernate's documents say

For example, if you cache results of a query against an object, Hibernate needs to keep track of whether any changes have been committed against the object, and invalidate the cache accordingly

If the answer for question 1 is YES(they are the entities' ID in query result), Does this mean the cached keep timestamps when a table was last updated and decide to refresh cache if needed?

Rob
  1. Yes.
  2. The cache keeps track of which tables a given cache entry is dependent on. When any of these tables are updated (any row or rows, whether or not they affect a particular cached result), the cache entry is invalidated. This means that the next time that query+parameters happens again, it will result in a database query rather than pulling the result from the query cache.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related