hibernate 二级缓存set更新问题

我的配置缓存有效,但set方法无法同步到数据库中。请看下面

<bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
                <prop key="hibernate.cache.use_second_level_cache">true</prop>   <!-- 设置缓存机制为二级缓存 -->
                <prop key="hibernate.cache.use_query_cache">true</prop>    <!-- 启动查询缓存 -->
                <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>  <!-- 设置二级缓存的Provider类 -->
                 <prop key="hibernate.cache.provider_configuration_file_resource_path">classpath:ehcache.xml</prop> <!--  设置缓存的配置文件路径 -->
            </props>
        </property>
        <property name="mappingLocations">
            <list>
                <value>classpath*:/com/sml/core/cache/entity/*.hbm.xml</value>
            </list>
        </property>
    </bean>

 

 

ehcache.xml

<ehcache>
 
     <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120"
        overflowToDisk="true" diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000"
        diskPersistent="false" diskExpiryThreadIntervalSeconds="120"/>
       
    <cache name="org.hibernate.cache.StandardQueryCache"
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="300"
        timeToLiveSeconds="4200"
        overflowToDisk="true"
        />
       
    <cache name="org.hibernate.cache.UpdateTimestampsCache"
        maxElementsInMemory="5000"
        eternal="true"
        timeToIdleSeconds="0"
        timeToLiveSeconds="0"
        overflowToDisk="false"
        />
       
     <cache name="com.sml.core.cache.entity.DStudent"
         maxElementsInMemory="1000"
        eternal="false"
        timeToIdleSeconds="100"
        timeToLiveSeconds="4200"
        overflowToDisk="true"
     />
</ehcache>

 

hbm.xml

 

<hibernate-mapping>
    <class name="com.sml.core.cache.entity.DStudent" table="D_STUDENT" schema="DARCY" dynamic-update="true">
    <cache usage="read-write"/>
        <id name="id" type="long">
            <column name="ID" precision="10" scale="0" />
            <generator class="native"></generator>
        </id>
        <property name="name" type="string">
            <column name="NAME" length="10" />
        </property>
        <property name="age" type="integer">
            <column name="AGE" precision="22" scale="0" />
        </property>
    </class>
</hibernate-mapping>

dao文件

@Repository("studentDao")
public class StudentDaoImpl extends BaseDao implements StudentDao {

    @Override
    public List<DStudent> findAll() {
        Query query = this.getSession().createQuery("from DStudent");
        query.setCacheable(true);
        return query.list();
    }

    @Override
    public DStudent findById(long id) {
        Query query =this.getSession().createQuery("from DStudent where id = :id").setParameter("id", id);
        query.setCacheable(true);
        return (DStudent) query.uniqueResult();
    }

 

 

action

@Resource(name="studentDao")
    private StudentDao studentDao;
   
    @RequestMapping("/t1")
    public String studentTest(){
       
        DStudent student = studentDao.findById(1);
        System.out.println(student.getName());
       
        System.out.println("------------------");
       
        student = studentDao.findById(1);
        student.setName("Olive");
        System.out.println(student.getName());
       
        return "index1";
    }

控制台

Hibernate:
    select
        dstudent0_.ID as ID0_,
        dstudent0_.NAME as NAME0_,
        dstudent0_.AGE as AGE0_
    from
        DARCY.D_STUDENT dstudent0_
    where
        dstudent0_.ID=?
olive
------------------
Olive

 

如上所示,缓存起了作用,但是set方法后,数据库中的数据没有更新,何解?求指导,求解答!谢谢!

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值