title: Hibernate second level cache with Spring
body: <p>I'm using Spring + JPA + Hibernate. I'm trying to enable Hibernate's second level cache. In my Spring's <code>applicationContext.xml</code> I have:</p>
  
  <pre><code>&lt;prop key="hibernate.cache.provider_class"&gt;net.sf.ehcache.hibernate.SingletonEhCacheProvider&lt;/prop&gt;
  &lt;prop key="hibernate.cache.provider_configuration_file_resource_path"&gt;/ehcache.xml&lt;/prop&gt;
  </code></pre>
  
  <p>When I run I get the error:</p>
  
  <pre><code>Caused by: org.hibernate.HibernateException: Could not instantiate cache implementation
       at org.hibernate.cache.CacheFactory.createCache(CacheFactory.java:64)
  
  Caused by: org.hibernate.cache.NoCachingEnabledException: Second-level cache is not enabled for usage [hibernate.cache.use_second_level_cache | hibernate.cache.use_query_cache]
       at org.hibernate.cache.NoCacheProvider.buildCache(NoCacheProvider.java:21) 
  </code></pre>
  
  <p>So it's complain I don't have second level cache enabled. I attempt to enable it by adding to my <code>applicationContext.xml</code>:</p>
  
  <pre><code>&lt;prop key="hibernate.cache.use_second_level_cache"&gt;true&lt;/prop&gt;
  </code></pre>
  
  <p>But still no joy. I also tried adding this to my ehcache.xml:</p>
  
  <pre><code>&lt;property name="hibernate.cache.use_second_level_cache"&gt;true&lt;/property&gt;
  </code></pre>
  
  <p>But it still doesn't work. Changing the <code>provider_class</code> to <code>org.hibernate.cache.EhCacheProvider</code> doesn't help either:</p>
  
  <pre><code>&lt;prop key="hibernate.cache.provider_class"&gt;org.hibernate.cache.EhCacheProvider&lt;/prop&gt;
  </code></pre>
  
  <p>My entity classes are annotated to use caching</p>
  
  <pre><code>@Cache(usage=CacheConcurrencyStrategy.READ_ONLY)
  </code></pre>
  
  <p>So, how do I enable second level cache?</p>
  
  <p><strong>Edit:</strong>  This is under the bean:</p>
  
  <pre><code>&lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"&gt;
  </code></pre>
  
  <p><strong>Resolved:</strong> Since I'm using <code>LocalEntityManagerFactoryBean</code> it gets its settings from <code>META-INF/persistence.xml</code>. My settings in <code>applicationContext.xml</code> weren't even being read.</p>