测试的控制器方法:
@ResponseBody @RequestMapping(value = "/testFlush/json", produces = SystemHWUtil.RESPONSE_CONTENTTYPE_JSON_UTF) public String json2(Model model, HttpServletRequest request, HttpServletResponse response , int id) {// Session session=this.houseEvaluateDao.getSessionFactory().getCurrentSession();// session.setFlushMode(FlushMode.MANUAL); HouseEvaluate houseEvaluate = this.houseEvaluateDao.getVariable(id);// session.setReadOnly(houseEvaluate,true); Double score = new Random().nextDouble(); System.out.println("score :" + score); houseEvaluate.setScore(score); HouseEvaluate houseEvaluate22 = this.houseEvaluateDao.getVariable(id - 1); houseEvaluate22.setScore(new Random().nextDouble()); this.houseEvaluateDao.update(houseEvaluate22); return BaseResponseDto.jsonValue(houseEvaluate); }
没有使用
OpenSessionInViewFilter
时,访问的sql:
Hibernate: select houseevalu0_.id as id1_32_0_, houseevalu0_.createTime as createTi2_32_0_, houseevalu0_.evaluateText as evaluate3_32_0_, houseevalu0_.evaluateType as evaluate4_32_0_, houseevalu0_.houseInfo_id as houseInf8_32_0_, houseevalu0_.orderNo as orderNo5_32_0_, houseevalu0_.score as score6_32_0_, houseevalu0_.updateTime as updateTi7_32_0_, houseevalu0_.userId as userId9_32_0_ from t_houseEvaluate houseevalu0_ where houseevalu0_.id=?03 三月 2018 12:12:17,460 TRACE org.hibernate.type.descriptor.sql.BasicBinder:81 - binding parameter [1] as [INTEGER] - [22]Hibernate: select houseevalu0_.id as id1_32_0_, houseevalu0_.createTime as createTi2_32_0_, houseevalu0_.evaluateText as evaluate3_32_0_, houseevalu0_.evaluateType as evaluate4_32_0_, houseevalu0_.houseInfo_id as houseInf8_32_0_, houseevalu0_.orderNo as orderNo5_32_0_, houseevalu0_.score as score6_32_0_, houseevalu0_.updateTime as updateTi7_32_0_, houseevalu0_.userId as userId9_32_0_ from t_houseEvaluate houseevalu0_ where houseevalu0_.id=?03 三月 2018 12:12:18,007 TRACE org.hibernate.type.descriptor.sql.BasicBinder:81 - binding parameter [1] as [INTEGER] - [21]Hibernate: /* update oa.entity.HouseEvaluate */ update t_houseEvaluate set createTime=?, evaluateText=?, evaluateType=?, houseInfo_id=?, orderNo=?, score=?, updateTime=?, userId=? where id=?03 三月 2018 12:12:18,615 TRACE org.hibernate.type.descriptor.sql.BasicBinder:81 - binding parameter [1] as [VARCHAR] - [2017-10-15 12:03:20]03 三月 2018 12:12:18,623 TRACE org.hibernate.type.descriptor.sql.BasicBinder:81 - binding parameter [2] as [VARCHAR] - [发布房源]03 三月 2018 12:12:18,624 TRACE org.hibernate.type.descriptor.sql.BasicBinder:81 - binding parameter [3] as [INTEGER] - [1]03 三月 2018 12:12:18,633 TRACE org.hibernate.type.descriptor.sql.BasicBinder:81 - binding parameter [4] as [INTEGER] - [144]03 三月 2018 12:12:18,634 TRACE org.hibernate.type.descriptor.sql.BasicBinder:69 - binding parameter [5] as [VARCHAR] - [null]03 三月 2018 12:12:18,641 TRACE org.hibernate.type.descriptor.sql.BasicBinder:81 - binding parameter [6] as [DOUBLE] - [0.7026864201132653]03 三月 2018 12:12:18,645 TRACE org.hibernate.type.descriptor.sql.BasicBinder:81 - binding parameter [7] as [VARCHAR] - [2017-10-15 12:03:20]03 三月 2018 12:12:18,649 TRACE org.hibernate.type.descriptor.sql.BasicBinder:81 - binding parameter [8] as [INTEGER] - [92]03 三月 2018 12:12:18,654 TRACE org.hibernate.type.descriptor.sql.BasicBinder:81 - binding parameter [9] as [INTEGER] - [21]
即查询两次,更新一次,这非常好理解,因为我只调用了一次update
使用OpenSessionInViewFilter 之后
并且设置
session.setFlushMode(FlushMode.MANUAL);
再次访问,会执行两次update,即也更新了houseEvaluate ,虽然没有显式地调用update
但是我的本意不想更新houseEvaluate,因为我没有手动调用update
怎么办呢?
解决方法:
1,查询对象时,默认采用只读方式
session.setDefaultReadOnly(true);
或者:
session.setReadOnly(entityObj, sessionParam.isReadonly());
2,对于要更新的对象,再修改之前,执行:
this.houseEvaluateDao.setReadOnly(houseEvaluate22,false);
即把persist 对象变为可写.
注意:
(1)过滤器OpenSessionInViewFilter 中依然设置setFlushMode(FlushMode.MANUAL);
(2)必须在对象属性修改之前调用setReadOnly(houseEvaluate22,false),否则无效,不会更新
(3)session的setDefaultReadOnly 值为true
java socket实现的简易的聊天工具demo 见