aspose file tools*
The moose likes Object Relational Mapping and the fly likes How to use hibernate query cache Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "How to use hibernate query cache" Watch "How to use hibernate query cache" New topic
Author

How to use hibernate query cache

ola gerchikov
Greenhorn

Joined: Nov 04, 2006
Posts: 5
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

Joined: Feb 05, 2001
Posts: 17250
    
    6

"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.


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

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

Joined: Nov 04, 2006
Posts: 5
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

Joined: Feb 05, 2001
Posts: 17250
    
    6

"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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to use hibernate query cache