近日新增数据报错:
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, "新增成功!");
}