简体   繁体   中英

Hibernate query cache with criteria

I think there is a point that I have not understand yet. I have a class witch have 3 collections of elements in attribute

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

I my DAO, I have a function that I call from a service layer

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

Actually, the only object put in cache is the only the Pays p, the others collections pc, ps and kf are not put in cache.

here is my 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>

My hibernate property

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

and my maven dependancy

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

How can I achieve that? I use FetchMde.Join because I want to avoid n+1 select, and actually my Pays is loaded in one select. Thank in advance.

Hibernate will Cache only the Pays p object, which is true, because Criteria created on that only. If you want to Cache other collections then you have to use Cachebale on getter method of collections property. You can also use Cache annotation on each Entity object as well.

I resolved my issues,simply by applying a filter in my web.xml like this :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM