日期:2014-05-17  浏览次数:20925 次

hibernate怎么清楚二级缓存????
hibernate怎么清楚二级缓存????

如下图:点击删除后 数据库中已经删除了,但页面却还是显示的,刷新页面也没用



下面是删除代码
public void delete(Customer persistentInstance) {
log.debug("deleting Customer instance");
try {
getHibernateTemplate().flush();
getHibernateTemplate().evict(persistentInstance);
getHibernateTemplate().clear();
getHibernateTemplate().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}

Customer.xml配置的是
<cache usage="read-write"/>

applicationContextl.xml
<!-- 把数据源注入到session工厂 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="datasource"></property>
<property name="mappingResources">
<list>
<value>org/hibernate/xml/Good.hbm.xml</value>
<value>org/hibernate/xml/Customer.hbm.xml</value>
<value>org/hibernate/xml/Orders.hbm.xml</value>
<value>org/hibernate/xml/OrderDetail.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${dialect}</prop>   
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<!-- Hibernate的缓存接口 -->
<prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop>
<!-- Hibernate的二级缓存打开 -->
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<!-- Hibernate的查询缓存打开 -->
<prop key="hibernate.cache.use_query_cache">true</prop>
<!-- Hibernate的统计信息打开 -->
<prop key="hibernate.generate_statistics">true</prop>
</props>
</property>
</bean>

------解决方案--------------------
sessionFactory.evict(Entity.class);//清除所有Entity
sessionFactory.evict(Entity.class,id);//清除指定Entity
比如:
//用SessionFacotry管理二级缓存
SessionFactory factory=HibernateUtils.getSessionFactory();
//evict()把id为1的Student对象从二级缓存中清除.
factory.evict(Student.class, 1); 



    
------解决方案--------------------