• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to use hibernate query cache

 
ola gerchikov
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have setup ehcache as hibernate query cache. But when I execute the
same query multiple times, it still go and query the database (from
the hibernate sql out, it still executes mysql query). Can you please
tell me what am i missing?


Here is why I did in my hibernate.cfg.xml file:
Add these:
<property name="cache.provider_class">
org.hibernate.cache.EhCacheProvider
</property>
<property name="hibernate.cache.use_query_cache">true</property>

And then, here is my query:
Query query = session
.createQuery(
"from Country as country where country.language= :language and
country.phone.id = honeid");

query.setString("language", language).setLong("phoneid",
phone.getId().longValue());

query.setCacheable(true);
// uncomment this still does not work.
// query.setCacheRegion("query.Builds");

List result = query.list();


Can you please tell my how to get ehcache to work as hibernate query cache.
I think I have followed what the documentation said.

Thank you.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"o gerchikov ",
Welcome to the JavaRanch.

We're a friendly group, but we do require members to have valid display names.

Display names must be two words: your first name, a space, then your last name. Fictitious names are not allowed.

Please edit your profile and correct your display name since accounts with display names get deleted.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to note, the query cache just stores the query and the ids in the result. The actual objects still need to be in the regular second level cache. If those objects are no longer there, then Hibernate has to get them from the database.

Mark
 
ola gerchikov
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help. Add "<cache usage="read-only>" to my mapping file works.

However, there is a case where I expect hibernate to execute the query from the database instead of pull from the query cache, it does not work.

Here is the situation
1. execute a query
2. execute the same query again with no update to the database (expected)
3. add a row in the database via a legarcy application (non hibernate application).
4. execute a query (expected to re-execute the query instead of pulling the result from the database) This part does not work.

In the hiberante-in-action book, it talks about 'timestamp cache' on page 291. But I can't an example about that. I appreciate if you can help me.

Thank you.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"add a row in the database via a legarcy application (non hibernate application)."

There is your answer. How can Hibernate know that that legacy app changed the data, it can't and therefore assumes the data is still up to date and doesn't need to go to the database.

Mark
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic