我的配置缓存有效,但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方法后,数据库中的数据没有更新,何解?求指导,求解答!谢谢!