我不知道为什么无论从前端传入的JSON数据中是否有主键,在添加数据时主键都是空的,我将主键设置为实体中的自动增长。
在手动输入数据库的主键之前,顺序是混乱的,并且是随机的。我不知道它是否会影响加法。
我的实体:
@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
{“组件”:“显示”、“图标”:“条”、"menuName":“测试”、"menuType":1、"ordernum":11、"parentId":0、"path":"mnm“、"perms":"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).]发布于 2021-11-07 11:21:50
很难知道@TableId到底在做什么,因为它是一些自定义注释。您可以使用
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;然后,根据您的DataBase创建序列或将此字段设置为自动增量。例如,在MySQL中,它将是:
alter table front_menu_table modify id long auto_increment;https://stackoverflow.com/questions/69870632
复制相似问题