I am using ehcache with hibernate in my application.
I am able to cache using ehcache but I am trying to achieve something like this:
-
I want to cache database tables which are frequently using in my application at first only.
-
After caching tables, how can i use it in my hql query.
like in my hql query i use Pojo class name for respective db table. In this case how can i use my hql query.
Please provide some documentation or support for above.
The documentation of ehcache explains this in detail.
Basically, you can use the
@Cacheannotation to tell Hibernate which type of access to cache.In the ehcache configuration, you can configure one cache per entity; the class name is the key. That allows you to define how many elements to cache per entity.
If you want Hibernate to cache queries, set the property
hibernate.cache.use_query_cachetotrue. Hibernate will then “do the right thing” when you use HQL.[EDIT]
You can try to load objects so they are in the cache at the time Hibernate needs them for some query.
But caching is a very complex topic. There are many factors which allow and prevent the use of a cache. If you just have simple cases, the caching will “just work” and you don’t need to do anything. If you have complex HQL, anything can happen. I have seen examples where the same query could take 100ms or 10s, depending on a value in the
whereclause. I’ve seen code where caching made the whole thing slower. I’ve seen code where we requested the underlying connection from the Hibernate session and did everything ourselves because Hibernate simply didn’t fit the bill.The usual approach is to make the code as simple as you can and run the result in production for a while. In time, users will start to complain about places where it is too slow. Then, you can optimize those few places. This way, you don’t waste time on code which users don’t care about.