Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring Data + Hibernate Query Caching not working
    primarykey
    data
    text
    <p>I am trying but not succeeding to cache a query in <strong>Spring Data</strong> and <strong>Hibernate</strong> environmet with following <strong>dependencies</strong> : </p> <pre><code>compile 'org.hibernate:hibernate-validator:4.0.0.GA' compile 'org.hibernate:hibernate-entitymanager:3.6.6.Final' compile 'org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.0.Final' compile 'org.hibernate:hibernate-ehcache:3.3.1.GA' compile 'org.springframework.data:spring-data-jpa:1.2.0.RELEASE' </code></pre> <h2>My Spring Data Repository(ServiceRepository) for entity Service is </h2> <pre><code>public interface ServiceRepository extends CrudRepository&lt;Service, Long&gt;, JpaSpecificationExecutor&lt;Service&gt; { @Cacheable("merchantServices") @Query("select s from Service s JOIN s.statusList sas where s.status=?1 and s.priviligedUser.priviligedUserType IN (2,4) and (s.id IN (select st.id from Service st inner join st.tags tag where tag IN (?3)) or s.serviceType IN (?2)) and sas.active=true and sas.transactorType=?4 ORDER BY s.name") List&lt;Service&gt; getAllMerchantServicesByStatusTypeAndTags(ServiceStatus status, List&lt;ServiceType&gt; type, List&lt;String&gt; tags, TransactorType transactor); } </code></pre> <h2>Repository's @Cacheable method being invoked from </h2> <pre><code>public List&lt;Service&gt; getAllServicesByStatusAndId(ServiceStatus status, List&lt;Long&gt; services, TransactorType transactor) { return serviceRepository.getAllMerchantServicesByStatusAndServiceId(status, services, transactor); } </code></pre> <h2>My Caching confing (jpa-context.xml) is </h2> <p>It is inspired from <a href="https://github.com/spring-projects/spring-data-jpa-examples/blob/master/spring-data-jpa-example/src/main/resources/caching-repository-context.xml" rel="nofollow noreferrer">spring-data-jpa-examples/src/main/resources/caching-repository-context.xml </a></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans xmlns:p="http://www.springframework.org/schema/p" xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd" default-autowire="byName"&gt; &lt;tx:annotation-driven /&gt; &lt;bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager"&gt; &lt;property name="caches"&gt; &lt;set&gt; &lt;bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="merchantServices"/&gt; &lt;/set&gt; &lt;/property&gt; &lt;/bean&gt; &lt;/beans&gt; </code></pre> <h2>cache enabled Hibernate config (hibernate.cfg.xml) is </h2> <pre><code>&lt;?xml version='1.0' encoding='utf-8'?&gt; &lt;!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt; &lt;hibernate-configuration&gt; &lt;session-factory&gt; &lt;property name="hibernate.default_batch_fetch_size"&gt;16&lt;/property&gt; &lt;property name="hibernate.max_fetch_depth"&gt;5&lt;/property&gt; &lt;property name="hibernate.cache.use_query_cache"&gt;true&lt;/property&gt; &lt;property name="hibernate.cache.use_second_level_cache"&gt;true&lt;/property&gt; &lt;property name="hibernate.generate_statistics"&gt;true&lt;/property&gt; &lt;property name="hibernate.cache.provider_class"&gt;org.hibernate.cache.EhCacheProvider&lt;/property&gt; &lt;property name="hibernate.c3p0.timeout"&gt;300&lt;/property&gt; &lt;/session-factory&gt; &lt;/hibernate-configuration&gt; </code></pre> <p>Looking at log; every time I request, I see query executed.</p> <pre><code>17:50:28.997 [http-bio-8080-exec-10] INFO org.hibernate.stat.Statistics - HQL: select s from Service s JOIN s.statusList sas where s.status=?1 and s.priviligedUser.priviligedUserType IN (2,4) and s.id IN (?2) and sas.active=true and sas.transactorType=?3 ORDER BY s.name, time: 57ms, rows: 1 </code></pre> <p>Next time I request, I see following query;</p> <pre><code>18:03:40.374 [http-bio-8080-exec-8] INFO org.hibernate.stat.Statistics - HQL: select s from Service s JOIN s.statusList sas where s.status=?1 and s.priviligedUser.priviligedUserType IN (2,4) and s.id IN (?2) and sas.active=true and sas.transactorType=?3 ORDER BY s.name, time: 47ms, rows: 1 </code></pre> <h2>References</h2> <p><a href="https://github.com/spring-projects/spring-data-jpa-examples/tree/master/spring-data-jpa-example/src/main/java/org/springframework/data/jpa/example/repository/caching" rel="nofollow noreferrer">spring-projects/spring-data-jpa-examples</a></p> <p><a href="http://www.captaindebug.com/2012/09/spring-31-caching-and-config.html#.Ujl-iIoW3uk" rel="nofollow noreferrer">Spring 3.1 Caching and Config</a></p> <p><a href="https://stackoverflow.com/a/18363810/432903">Spring Data Rest - Caching</a></p> <p><a href="https://stackoverflow.com/q/17896118/432903">Spring Data Repository caching results</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload