Task: make basic methods of spring jpa data cacheable (using hibernate/jpa), like
Page<T> findAll(Pageable pageable)
List<T> findAll();
etc
and do it on some kind of top generic interface level, without custom dao implementations.
This is continuation of original topic How to add QueryHints on Default Spring Data JPA Methods?
I haven't still found the solution, but tried to solve it with different ways, including adding annotations like
@javax.persistence.Cacheable
and @org.hibernate.annotations.Cache
on data model class.
Here are some excerpts of my config:
pom.xml (taken from here):
... <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-ehcache</artifactId> <version>4.1.9.Final</version> <exclusions> <exclusion> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-core</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>2.7.0</version> </dependency> ... <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jpa</artifactId> <version>1.4.2.RELEASE</version> </dependency> ...
applicationContext.xml:
<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/> <bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory"/> </bean> <bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory"> <property name="persistenceUnitName" value="persistenceUnit"/> <property name="dataSource" ref="dataSource"/> </bean>
persistence.xml:
... <provider>org.hibernate.ejb.HibernatePersistence</provider> <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode> <properties> ... <property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory"/> <property name="hibernate.cache.use_second_level_cache" value="true"/> <property name="hibernate.cache.use_query_cache" value="true" /> </properties> ...
Apart from all above I have configured spring 3.2 cache, but eventually I want to have a solution not based on spring cache, so at the moment I don't use spring cache config.
My model looks like:
@Entity @Table(name = "ABC") @Cacheable(true) @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) public class ABC { ...
My parent generic DAO looks like:
public interface CacheableGenericDao<T, ID> extends JpaRepository<T, ID>, JpaSpecificationExecutor<T> { List<T> findAll(); Page<T> findAll(Pageable pageable); <S extends T> S save(S entity); ...
P.S. Here is one more useful link concerning the topic, but I do want use basic method names.
So what am I missing conceptually? Is there any approach at all or do I want too much?
this is how the things seem to go: jira.springsource.org/browse/DATAJPA-173