SpringMVC修改外键时的小细节(500错误identifier of an instance of cn.liangqinghai.beans.Teacher was altered from 1 to 2)
SpringMVC页面中,把关联的外键修改,会出错:
错误描述,在关联的使实体内修改外键,可认为修改关联表的主键
解决办法:把关联的对象修改为空,让MVC重新设置新的关联对象
代码
@ModelAttribute public void getStudent(@RequestParam(value="sid",required=false) Integer sid,Map<String, Object> map){ if(sid != null){ Student student = studentService.getBySid(sid); student.setTeacher(null); map.put("student", student); } }
记得要把student对象重新放入请求域中