近日新增数据报错:
could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement.
该错误是关联的用户名不能为空,因此直接在该控制层,新增数据的方法那儿,增加一句代码,新增当前登录用户为使用者。我的代码如下:
@ResponseBody
@RequestMapping("/save")
public Result save(Stockincomebill stockincomebill){
try {
stockincomebill.setAuditor(ShiroSessionUtils.getUser());//添加这句代码
stockincomebillService.save(stockincomebill);
} catch (Exception e) {
e.printStackTrace();
return new Result(false, "新增失败-原因是:" + e.getMessage());
}
return new Result(true, "新增成功!");
}
本文介绍了一种在新增数据过程中遇到的约束违反异常,并提供了解决方案:通过在控制层添加当前登录用户的代码来避免关联用户名为空的问题。
476

被折叠的 条评论
为什么被折叠?



