Configuring Ehcache with JPA, Hibernate & Spring

This post details on the steps required to configure ehcache as the second level hibernate cache when jpa, hibernate and spring are used in combination

Configuring the cache

Add the below properties to the persistence.xml configuration file

<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.cache.use_query_cache" value="true"/>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider"/>
<property name="hibernate.generate_statistics" value="true"/>

EHCache comes with some sensible defaults out of the box when you configure caching. But you can create your own configuration by creating a file named ehcache.xml and placing it in your class path. The xml file can contain

<cache name="samples.Employee"

Configuring the Entity

Add the below annotation on your mapped entity

@org.hibernate.annotations.Cache(usage = org.hibernate.annotations.CacheConcurrencyStrategy.READ_WRITE)

You have now configured the cache for loading a single object at a time. For example the loadById kind of methods. When you are doing more like finder queries, query by example or using Criteria queries, you are not using second level cache. So if we have a queries that we execute a lot, we need to use the query cache.

Query Cache

To enable query caching, you need to make two configurations. One is to enable the property “hibernate.cache.use_query_cache” as we did above and second is to set the query cache hint as show below

        Query query = entityManager.createQuery("from " + Employee.class.getName());
        query.setHint("org.hibernate.cacheable", true);
        return query.getResultList();

Does you cache work? View statistics

Execute the below code to print the cache statistics

EntityManagerFactoryInfo entityManagerFactoryInfo =
(EntityManagerFactoryInfo) applicationContext.getBean("entityManagerFactory");
EntityManagerFactory emf = entityManagerFactoryInfo.getNativeEntityManagerFactory();
EntityManagerFactoryImpl emfImp = (EntityManagerFactoryImpl)emf;

Do not forget to enable the generation of statistics in your persistence.xml as described in the cache configuration above.


Posted on November 8, 2011, in ehcache, hibernate, jpa, spring and tagged . Bookmark the permalink. 3 Comments.

  1. can i have one showcase with full source code…i have tried with this ..but getting one exception saying ‘Second level cache in not enable’…

  2. Indeed, do you have a sample code of this one that works? Please post! It would be helpful.

  3. Hi there! This post couldn’t be written any better! Reading this post reminds me of my good old room mate! He always kept talking about this. I will forward this write-up to him. Pretty sure he will have a good read. Many thanks for sharing!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: