SSH Hibernate4配置缓存无效

南猿北蛰 2016-10-24 02:12:31
SSH 配置ehcache缓存无效,希望大家帮助一下,配置如下:

1.spring

	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="sysDataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
<prop key="cache.use_query_cache">true</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="cache.use_structured_entries">true</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
<prop key="net.sf.ehcache.configurationResourceName">ehcache.xml</prop>

</props>
</property>
<!-- 按照包路径自动绑定hibernate实体 -->
<property name="packagesToScan" value="com.bobo.app.model"></property>

</bean>


2.ehcache配置


<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<diskStore path="java.io.tmpdir" />
<!--缓存最大数目 -->
<!--缓存是否持久 -->
<!--是否保存到磁盘,当系统当机时 -->
<!--当缓存闲置 n 秒后销毁 -->
<!--当缓存存活 n 秒后销毁 -->
<defaultCache
maxElementsInMemory="10000"
eternal="true"
overflowToDisk="true"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
diskPersistent="false"
diskExpiryThreadIntervalSeconds= "120" />

</ehcache>


3.实体配置


import javax.persistence.Cacheable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;


@Entity
@Table(name = "student")
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Student implements java.io.Serializable{

private static final long serialVersionUID = 1L;

private int id;
private String name;
private int age;
private String sex;
...


4.测试结果,每次查询都有 sql打印,说明缓存失败

Hibernate:
select
student0_.id as id1_0_,
student0_.age as age2_0_,
student0_.name as name3_0_,
student0_.sex as sex4_0_
from
student student0_
...全文
111 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

67,549

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧