java - Spring Boot + PostgreSQL: Cause: org.postgresql.util.PSQLException: ERROR: null value in column "front_menu_id" 违反非空约束
问题描述
不知道为什么不管前端传入的json数据中是否有主键,添加数据的时候主键都说是空的,我在Entity里面设置了主键为自动增长
在手动输入数据库的主键之前,顺序是混乱的,数字是随机的。不知道会不会影响添加。
我的实体:
@Data
@EqualsAndHashCode(callSuper = true)
@TableName(value = "front_menu_table")
public class FrontMenuTable extends BaseEntity {
private static final long serialVersionUID = 1L;
@TableId(value = "front_menu_id", type = IdType.AUTO)
private Long frontMenuId;
@NotNull(message = "上级菜单不能为空")
private Long parentId;
@NotBlank(message = "菜单名称不能为空")
private String menuName;
private String path;
@NotBlank(message = "菜单授权码不能为空")
private String perms;
private String component;
@NotNull(message = "菜单类型不能为空")
private Integer menuType;
private String icon;
private Integer ordernum;
private Integer statu;
@TableField(exist = false)
private List<FrontMenuTable> children = new ArrayList<>();
我的控制器:
@PostMapping("/save")
@PreAuthorize("hasAuthority('sys:menu:save')")
public ApiRestResponse save(@Validated @RequestBody FrontMenuTable frontMenuTable,
Principal principal) {
frontMenuTable.setCreater(principal.getName());
frontMenuTable.setCreateTime(LocalDateTime.now());
frontMenuTableService.save(frontMenuTable);
return ApiRestResponse.success(frontMenuTable);
}
JSON
{ "component": "show", "icon": "bars", "menuName": "TEST", "menuType": 1, "ordernum": 11, "parentId": 0, "path": "mnm" , "烫发": "sys:yyy", "statu": 0 }
错误信息
2021-11-07 16:22:21.889 WARN 43898 --- [nio-8080-exec-6] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [org.springframework.dao.DataIntegrityViolationException:
### Error updating database. Cause: org.postgresql.util.PSQLException: ERROR: null value in column "front_menu_id" violates not-null constraint
详细:Failing row contains (null, 0, TEST, mnm, sys:yyy, show, 1, bars, 11, 0, jiangwen, 2021-11-07 16:22:21.87667, null, null).
### The error may exist in org/jiangwen/mapper/FrontMenuTableMapper.java (best guess)
### The error may involve org.jiangwen.mapper.FrontMenuTableMapper.insert-Inline
### The error occurred while setting parameters
### SQL: INSERT INTO front_menu_table ( parent_id, menu_name, path, perms, component, menu_type, icon, ordernum, statu, creater, create_time ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
### Cause: org.postgresql.util.PSQLException: ERROR: null value in column "front_menu_id" violates not-null constraint
详细:Failing row contains (null, 0, TEST, mnm, sys:yyy, show, 1, bars, 11, 0, jiangwen, 2021-11-07 16:22:21.87667, null, null).
; ERROR: null value in column "front_menu_id" violates not-null constraint
详细:Failing row contains (null, 0, TEST, mnm, sys:yyy, show, 1, bars, 11, 0, jiangwen, 2021-11-07 16:22:21.87667, null, null).; nested exception is org.postgresql.util.PSQLException: ERROR: null value in column "front_menu_id" violates not-null constraint
详细:Failing row contains (null, 0, TEST, mnm, sys:yyy, show, 1, bars, 11, 0, jiangwen, 2021-11-07 16:22:21.87667, null, null).]
解决方案
很难说到底@TableId
在做什么,因为它是一些自定义注释。您可以使用
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
然后,根据您的数据库创建一个序列或将此字段设置为自动递增。例如在 MySQL 中它将是:
alter table front_menu_table modify id long auto_increment;
推荐阅读
- python - Django Web 应用程序中的属性错误
- firebase - 我在 Flutter 应用程序中不断收到关于 Firestore 的 NoSuchMethodError 调用
- javascript - Jest and Playwright:访问 youtube.com 并搜索视频
- powershell - PowerShell 中的调用表达式无法识别运算符
- c# - .net core 3.1 调用 SendGrid 键的用户机密返回空值
- reactjs - npm run build & npm start 不工作,react app
- java - 如何在 NetBeans 中打印东西?
- javascript - 使用 replace 更改所有匹配项
- google-maps - 如何为谷歌地图图层中的所有标记设置标签颜色?
- r - 用 R 中的分组数据计算(类似于)移动平均线?