• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Behaviour of hibernate query cache for cacheable and non-cacheable entity

 
Bhagwat Reddy
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I am developing a web application using Spring 2.5.6, Hibernate 3.3. I want to implement cache mechanism using query cache in my application.So I have question regarding the same:

As to make a use of query cache we also need to enable second level cache, so I have enabled same and I am maintaining second level cache at entity level.I have 2 entities 1) Plan 2) Fund. Now I have enabled cache for Plan entity by writing Cache usage tag in there hbm file and not enable for Fund entity. I am writing join query to get data from this 2 tables and set that query as Cacheable.

So how it stores data internally? is only Plan data is store in second level cache and to retrieve Fund data it always hit database?

Thanks.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bhagwat,
Welcome to CodeRanch!

This thread discusses the relationship between the two caches nicely. I like this sentence from it:
the query cache makes the query cachable but the secong level cache makes the Entities catchable based on their identifier(pk)
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also went into quite a bit of detail in this post
http://www.coderanch.com/t/590423/ORM/databases/Caching-Technique-Hibernate
 
Bhagwat Reddy
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jeanne and Bill for your reply.

Bill from your post I can understand that Cachable entities are storing into second level cache and they are retrieving from there by using primary key which is stored in query cache and for non-cachable entities nothing is storing in second level cache so it is retrieved from database by executing sql query for each entity. If I am wrong please correct me.

Now If Plan entity has 3 columns and Fund entity has 5 columns so in hql query I am getting 2 columns value of plan which are other than primary key column and 3 columns value of Fund table by joining this two entity. so in this case why Plan entity is not adding into second level cache which is cachable entity and for Fund why it is not hitting to database which is non-cacheble entity? Results are retrieved directly from query cache instead of looking into second level cache for cachable entity and by hitting database for non-cachable entity.

This behavior I can see in log file.

thanks.
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you using for you cache? Assuming it is configured to use some sort of a disk store have a look at the file and you can see what exactly is being written to the cache. You can also bump up the logging level for the necessary packages for your cache provider.
 
Bhagwat Reddy
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using query cache to load look-up data which is static and change very less frequently.

In log I can see that when I try to execute "From Plan P" query which is cachable then it adds all Plan entity into second level cache and when I execute "Select p.PlanId,p.PlanName from Plan p" which is also cachable query then it is not adding Plan entity into second level cache. On next time hit For 2nd query, data is getting from query cache not from second level cache. So can you please able to tell me why it is not adding Plan entity into second level cache for 2nd query?

Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic