Hibernate:启用二级缓存时出现问题

Hibernate:启用二级缓存时出现问题,hibernate,Hibernate,我正在使用Hibernate 3.3.4.GA。在我们的hibernate.cfg.xml文件中,我们指定 <property name="hibernate.cache.provider_class">net.sf.ehcache.hibernate.SingletonEhCacheProvider</property> <property name="hibernate.cache.use_query_cache">true</pro

我正在使用Hibernate 3.3.4.GA。在我们的hibernate.cfg.xml文件中,我们指定

    <property name="hibernate.cache.provider_class">net.sf.ehcache.hibernate.SingletonEhCacheProvider</property>
    <property name="hibernate.cache.use_query_cache">true</property>
    <property name="hibernate.cache.use_second_level_cache">true</property>
    <property name="hibernate.cache.provider_configuration">classpath:ehcache.xml</property>
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.format_sql">true</property>
    <property name="hibernate.use_sql_comments">true</property>
    <property name="hibernate.generate_statistics">true</property>
    <property name="hibernate.cache.use_structured_entries">true</property>
下面是我们用于检索结果的方法。知道为什么未命中计数和命中计数都是零吗

@Entity
@Cache(usage=CacheConcurrencyStrategy.READ_ONLY)
@Table(name="LEAD")
public WebLeads getLeads(WebLead webLead) {
    final Session session = sessionFactory.openSession();
    final Criteria crit = session.createCriteria(WebLead.class);
    crit.setCacheable(Boolean.TRUE);

    // Find webLeads matching input
    crit.add( Example.create(webLead) );
    // Make special exception for primary key since that isn't covered by Example.create
    if (webLead.getLEAD_ID() != null) { 
        crit.add(Restrictions.eq("LEAD_ID", webLead.getLEAD_ID()));
    }   // if

    @SuppressWarnings("unchecked")
    final List<WebLead> results = crit.list();
    final WebLeads webLeads = new WebLeads();
    webLeads.addAll( results );

    session.close();
    return webLeads;
}
@实体
@缓存(用法=缓存并发策略。只读)
@表(name=“LEAD”)
公共WebLeads getLeads(WebLead WebLead){
final Session Session=sessionFactory.openSession();
最终标准crit=session.createCriteria(WebLead.class);
临界值可设置缓存(布尔值为真);
//查找与输入匹配的WebLead
crit.add(Example.create(webLead));
//对主键做一个特殊的例外,因为Example.create中没有涉及到这一点
如果(webLead.getLEAD_ID()!=null){
crit.add(Restrictions.eq(“LEAD_ID”,webLead.getLEAD_ID());
}//如果
@抑制警告(“未选中”)
最终列表结果=crit.List();
最终WebLeads WebLeads=新WebLeads();
webLeads.addAll(结果);
session.close();
返回网络线索;
}

很明显缓存没有启用,但我不知道为什么。-Dave

请在此处查看我的答案,以了解正在运行的二级缓存配置:

我希望有帮助

更新:

试试这个:

Statistics stats = sessionFactory.getStatistics();
stats.setStatisticsEnabled(true);
SecondLevelCacheStatistics cacheStats = stats.getSecondLevelCacheStatistics(cacheRegion);

您使用的是Hibernate 3.3.4,但配置的是
cache.provider\u class
,它是针对
3.3之前的Hibernate


查看此链接以了解更多详细信息:

我的配置与此相同(除了禁用统计信息),但仍不起作用。为什么未命中率和命中率总是为零?如果我们没有命中缓存,那么未命中不应该是一个数字吗?您没有错过缓存实体上的注释或ehcache.xml中的xml片段吗?这个例子很适合我。我不知道有什么问题。我编辑了我的回复——我有注释。我认为缓存工作正常,但统计数据包不工作——否则为什么命中和未命中总是返回零?如何启用SecondLevelCacheStatistics?请尝试我答案中的更新部分。我认为这将有助于统计。
Statistics stats = sessionFactory.getStatistics();
stats.setStatisticsEnabled(true);
SecondLevelCacheStatistics cacheStats = stats.getSecondLevelCacheStatistics(cacheRegion);