2013-11-03 68 views
0

任务的:让春天JPA数据高速缓存的基本方法(使用Hibernate/JPA),像缓存默认春数据JPA方法

Page<T> findAll(Pageable pageable) 
List<T> findAll(); 
etc 

,并做它在某种顶级通用接口水平,没有自定义DAO实现。

这是原来的话题延续 How to add QueryHints on Default Spring Data JPA Methods?

我还没有发现仍然解决,但试图用不同的方法来解决这个问题,包括添加注释像 数据模型类@javax.persistence.Cacheable@org.hibernate.annotations.Cache


这里是我的配置中的一些摘录:

  1. 的pom.xml(从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> 
    ... 
    
  2. 的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> 
    
  3. 周的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> 
    ... 
    
  4. 除了上述所有我已经配置弹簧3.2缓存,但最终我希望能有一个解决方案不是基于弹簧缓存,因此在现阶段,我不使用弹簧缓存配置。

  5. 我的模型看起来像:

    @Entity 
    @Table(name = "ABC") 
    @Cacheable(true) 
    @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) 
    public class ABC { 
    ... 
    
  6. 我的父母了通用的DAO的样子:

    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); 
        ... 
    

附:这里有一个关于主题的更有用的link,但我确实需要使用基本的方法名称。

那么我在概念上缺少什么?有没有办法,或者我想要太多?

回答