缓存默认的Spring Data JPA方法 [英] Caching of default Spring Data JPA Methods
问题描述
任务:使spring jpa数据的基本方法可缓存(使用hibernate/jpa),例如
Task: make basic methods of spring jpa data cacheable (using hibernate/jpa), like
Page<T> findAll(Pageable pageable)
List<T> findAll();
etc
并在某种顶级通用接口级别上执行此操作,而无需自定义dao实现.
and do it on some kind of top generic interface level, without custom dao implementations.
这是原始主题的延续 如何在默认Spring Data JPA方法上添加QueryHint? /a>
This is continuation of original topic How to add QueryHints on Default Spring Data JPA Methods?
我还没有找到解决方案,但是尝试以不同的方式解决它,包括添加注释,例如
@javax.persistence.Cacheable
和@org.hibernate.annotations.Cache
在数据模型类上.
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.
以下是我的配置摘录:
-
applicationContext.xml:
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:
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> ...
-
除了上述所有内容之外,我还配置了spring 3.2缓存,但最终我希望有一个不基于spring缓存的解决方案,因此目前我不使用spring缓存配置.
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.
我的模型如下:
@Entity @Table(name = "ABC") @Cacheable(true) @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) public class ABC { ...
-
我的父类通用DAO如下:
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.这是关于该主题的另一个有用的链接,但我确实想使用基本方法名称.
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?
推荐答案
事情似乎是这样的: 查看全文