电力项目八--运行监控的保存
需求分析:保存运行监控文本内容到数据库,并再次查询后回显。
页面中调用保存的函数
function checkchar(){ if(document.Form2.stationRun.value.length>2500){ alert("站点运行情况字数不能超过2500字"); return; } if(document.Form2.devRun.value.length>2500){ alert("设备运行情况字数不能超过2500字"); return; } document.Form2.action="${pageContext.request.contextPath}/system/elecCommonMsgAction_save.do"; document.Form2.submit(); /* alert(" 待办事宜保存成功!"); */ }
将数据提交到controller中对应的 elecCommonMsgAction_save.do
然后重定向到/system/actingIndex.jsp ,再查询,对应的struts.xml配置:
<result name="save" type="redirectAction"> <param name="actionName">elecCommonMsgAction_home.do</param> </result>
controller中的操作:
1.查询数据库运行监控表中的数据,返回List,用来判断数据是否存在:
如果数据存在,组织PO对象,执行update操作;
如果数据不存在,组织PO对象,执行save操作。
则对应的控制层的写法:
/** * @Name:save * @Description: 运行监控数据 * @author kj * @version:V1.00 * @create Date:20170521 * @return String 重定向到system/actingIndex.jsp(再查询) */ public String save(){ elecCommonMsgService.saveElecCommonMsg(elecCommonMsg); return "save"; }
Service层:
注意:此处使用 Hibernate的快照进行更新,如果使用了update操作,
(不合法)使用update更新(Session中不允许出现2个相同的OID)
会报错:
a different object with the same identifier value was already associated with the session: [com.elec.domain.ElecCommonMsg#297e35035c28c368015c28c3e6780001]
/** * @Name saveElecCommonMsg * @Description: 保存运行监控表的数据 * @author kj * @version: V1.00 * @create Date: 2017-05-21 * @Parameters: saveElecCommonMsg:VO对象 * @return 无 * 此处需要保存或者更新,要加上事务 */ @Override @Transactional(readOnly=false) public void saveElecCommonMsg(ElecCommonMsg elecCommonMsg) { // 1.查询数据库运行监控表的数据,返回List,用来判断数据是否存在 List<ElecCommonMsg> list = elecCommonMsgDao.findCollectionByConditionNoPage("", null, null); //存在就更新 if(list != null && list.size() > 0 ){ /** 这么写会报错: * elecCommonMsg.setCreateDate(new Date()); elecCommonMsgDao.update(elecCommonMsg); 报错: * Struts Problem Report Struts has detected an unhandled exception: Messages: The given object has a null identifier: com.elec.domain.ElecCommonMsg The given object has a null identifier: com.elec.domain.ElecCommonMsg; nested exception is org.hibernate.TransientObjectException: The given object has a null identifier: com.elec.domain.ElecCommonMsg */ /** 这么写也报错: * (不合法)使用update更新(Session中不允许出现2个相同的OID) * 报错: ElecCommonMsg cmg = list.get(0); elecCommonMsg.setComID(cmg.getComID()); elecCommonMsg.setCreateDate(new Date()); elecCommonMsgDao.update(elecCommonMsg); * Struts has detected an unhandled exception: Messages: a different object with the same identifier value was already associated with the session: [com.elec.domain.ElecCommonMsg#297e35035c28c368015c28c3e6780001] a different object with the same identifier value was already associated with the session: [com.elec.domain.ElecCommonMsg#297e35035c28c368015c28c3e6780001]; nested exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.elec.domain.ElecCommonMsg#297e35035c28c368015c28c3e6780001] */ //使用快照进行更新: ElecCommonMsg ecg = list.get(0); ecg.setStationRun(elecCommonMsg.getStationRun()); ecg.setDevRun(elecCommonMsg.getDevRun()); ecg.setCreateDate(new Date()); }else{//否则就保存 elecCommonMsg.setCreateDate(new Date()); elecCommonMsgDao.save(elecCommonMsg); } }
此时保存成功。
关于hibernate的缓存问题,查看文档 http://blog.csdn.net/defonds/article/details/2308972 hibernate一级缓存和二级缓存的区别