File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes Second level cache does not work Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Second level cache does not work" Watch "Second level cache does not work" New topic

Second level cache does not work

Alex Bokov

Joined: Jun 01, 2009
Posts: 16
I am trying to make work hibernate second level caching not successfully.

I have:


and every time I call findAll() in CategoryService it calls data base, at least I see query log.

Is there some thing that I missed?
How to get caching work?

Thanks for any help.

Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

Only certain method hit the cache. From the documentation:

Whenever you pass an object to save(), update() or saveOrUpdate() and whenever you retrieve an object using load(), get(), list(), iterate() or scroll(), that object is added to the internal cache of the Session.

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Alex Bokov

Joined: Jun 01, 2009
Posts: 16
I found that caching works for single element

with session factory:
and with entity manager:

but does not work for lists

with session factory:
and entity manager:
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

Read Paul's post again.

Anytime you run a query whether from Criteria, HQL, or SQL Hibernate will NOT get data from the second level cache.

But if you turn on the second level query cache, then some queries can go to the query cache. In the query cache only IDs are stored. And there is a requirement that the Objects those IDs represent must be in the standard second level cache in a cache region.

One issue you have with the query cache is that the resultset could get stale real quickly if data is inserted or deleted from the database.


Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
Alex Bokov

Joined: Jun 01, 2009
Posts: 16
query caching is working

but hibernate need to be kicked

with session factory

with entity manager
I agree. Here's the link:
subject: Second level cache does not work
It's not a secret anymore!