简体   繁体   English

带有条件的休眠查询缓存

[英]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 我有一个女巫,在属性中有3个元素集合

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 我的DAO,我有一个从服务层调用的函数

 @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. 实际上,放入缓存的唯一对象是唯一的Pays p,其他集合pc,ps和kf不会放入缓存。

here is my ehCache.xml 这是我的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. 我使用FetchMde.Join是因为我想避免n + 1选择,而实际上我的薪水是一次选择加载的。 Thank in advance. 预先感谢。

Hibernate will Cache only the Pays p object, which is true, because Criteria created on that only. Hibernate将仅缓存Pays p对象,这是正确的,因为仅在该对象上创建了Criteria。 If you want to Cache other collections then you have to use Cachebale on getter method of collections property. 如果要缓存其他集合,则必须在collections属性的getter方法上使用Cachebale。 You can also use Cache annotation on each Entity object as well. 您还可以在每个Entity对象上使用Cache注释。

I resolved my issues,simply by applying a filter in my web.xml like this : 我解决了我的问题,只需在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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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