2013-04-16 3 views
0

나는 아직 이해하지 못했던 점이 있다고 생각한다. 내가 클래스 마녀가 내가 내 DAO는 유일한 객체가 캐시에 넣어, 내가기준을 가진 Hibernate 질의 캐시

@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; } 

실제로 서비스 계층에서 호출 기능을 한

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

속성에 요소의 3 개 컬렉션이 유일한 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> 

내 최대 절전 모드 속성

<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> 

내가 어떻게 달성 할 수 있습니까? n + 1 select를 피하기 위해 FetchMde.Join을 사용합니다. 실제로 Pay는 하나의 select에로드됩니다. 감사합니다.

답변

0

는 단순히 같은 내 web.xml 파일에 필터를 적용하여, 내 문제를 해결 각 컬렉션의,하지만 작동하지 않는 것 같습니다
0

Hibernate는 Pays p 오브젝트만을 캐시합니다. 왜냐하면 Criteria가 그것에 만 생성 되었기 때문입니다. 다른 컬렉션을 캐시하려면 컬렉션 속성의 getter 메서드에서 Cachebale을 사용해야합니다. 각 Entity 객체에서도 Cache annotation을 사용할 수 있습니다.

<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

이미 게터에 각 개체에'@Cache (사용 = CacheConcurrencyStrategy.READ_WRITE)를'추가 : – user1310305

+0

스프링 캐싱이나 EHCaching을 사용하고 있습니까? –

+0

EhCaching을 사용하고 있습니다 – user1310305

관련 문제