2013-04-16 57 views
0

我觉得这是我还没有明白点。 我有一个类巫婆有要素的3个集在属性与标准的Hibernate查询缓存

public class Pays implements Serializable{ 
private static final long serialVersionUID = -8767337896773261244L; 
/** 
* the id of the country 
*/ 
private long id; 

/** 
* name of the country 
*/ 
private String nom; 
/** 
* The region of the country 
*/ 
private Region region; 
/** 
* Relations between a country and a composant 
*/ 
private Set<PaysComposants> pc = new HashSet<PaysComposants>(0); 
/** 
* Relations between a country and a service 
*/ 
private Set<PaysServices> ps = new HashSet<PaysServices>(0); 
/** 
* Relations between a country and some keys figures 
*/ 
private Set<KeyFigure> kf = new HashSet<KeyFigure>(0); 

我吾道,我有我从服务层调用

@Override 
public Pays read(String nomPays) { 
    Criteria criteria = super.getSession().createCriteria(Pays.class); 
    criteria.setFetchMode("pc", FetchMode.JOIN); 
    criteria.setFetchMode("ps", FetchMode.JOIN); 
    criteria.setFetchMode("kf", FetchMode.JOIN); 
    criteria.add(Restrictions.eq("nom", nomPays)); 
    criteria.setCacheable(true); 
    Pays p=(Pays) criteria.uniqueResult(); 
    return p; } 

其实功能,放在缓存中的唯一对象是唯一的Pays p,其他集合pc,ps和kf不会放入缓存中。

这里是我的ehcache.xml中

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true" 
monitoring="autodetect" dynamicConfig="true"> 

<defaultCache maxElementsInMemory="100000" eternal="false" 
    timeToIdleSeconds="3600" timeToLiveSeconds="3600" overflowToDisk="true" /> 

<cache name="org.hibernate.cache.internal.StandardQueryCache" 
    maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="3600" 
    timeToLiveSeconds="3600"> 
</cache> 

<cache name="org.hibernate.cache.spi.UpdateTimestampsCache" 
    maxElementsInMemory="10000" eternal="true"> 
</cache> 

我的Hibernate属性

<property name="hibernateProperties"> 
     <props> 
      <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
      <prop key="hibernate.show_sql">true</prop> 
      <prop key="hibernate.hbm2ddl.auto">update</prop> 
      <prop key="current_session_context_class">thread</prop> 
      <prop key="hibernate.cache.use_query_cache">true</prop> 
      <prop key="hibernate.cache.use_second_level_cache">true</prop> 
      <prop key="hibernate.cache.use_structured_entries">true</prop> 
      <prop key="hibernate.cache.generate_statistics">true</prop> 
      <prop key="hibernate.cache.provider_class"> 
       org.hibernate.cache.EhCacheProvider 
      </prop> 
      <prop key="hibernate.cache.region.factory_class"> 
       org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory 
      </prop> 
      <prop key="hibernate.cache.provider_configuration_file_resource_path"> 
       applicationContext-ehCache-entity.xml 
      </prop> 

     </props> 
    </property> 

和我的行家扶养

<dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-ehcache</artifactId> 
     <version>4.2.0.Final</version> 
    </dependency> 

我怎样才能做到这一点?我使用FetchMde.Join,因为我想避免n + 1选择,实际上我的支付是在一个选择中加载的。 提前致谢。

回答

0

我解决我的问题,只是在我的web.xml这样的应用过滤器:

<filter> 
    <filter-name>openSessionInViewFilter</filter-name> 
    <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> 
    <init-param> 
     <param-name>sessionFactoryBeanName</param-name> 
     <param-value>baselineSessionFactory</param-value> 
    </init-param> 
</filter> 

<filter-mapping> 
    <filter-name>openSessionInViewFilter</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 
0

Hibernate将缓存只有自付p对象,这是真的,因为标准创建的,只有。如果你想缓存其他集合,那么你必须在collection属性的getter方法上使用Cachebale。 您还可以使用缓存标注每一个实体对象上也是如此。

+0

我已经在每个实体添加此'@Cache(使用率= CacheConcurrencyStrategy.READ_WRITE)'并且还取决于吸气剂每次收集的,但它似乎不是你使用Spring缓存或EHCaching工作 – user1310305

+0

? –

+0

我使用EhCaching – user1310305