首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring +PostgreSQL:原因: org.postgresql.util.PSQLException: ERROR: front_menu_id列中的空值违反了null约束

问Spring +PostgreSQL:原因: org.postgresql.util.PSQLException: ERROR: front_menu_id列中的空值违反了null约束
EN

Stack Overflow用户
提问于 2021-11-07 07:51:21
回答 1查看 550关注 0票数 1

我不知道为什么无论从前端传入的JSON数据中是否有主键,在添加数据时主键都是空的,我将主键设置为实体中的自动增长。

在手动输入数据库的主键之前,顺序是混乱的,并且是随机的。我不知道它是否会影响加法。

我的实体:

代码语言:javascript
复制
@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<>();

我的控制器:

代码语言:javascript
复制
@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}

错误消息

代码语言:javascript
复制
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).]
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-07 11:21:50

很难知道@TableId到底在做什么,因为它是一些自定义注释。您可以使用

代码语言:javascript
复制
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;

然后,根据您的DataBase创建序列或将此字段设置为自动增量。例如,在MySQL中,它将是:

代码语言:javascript
复制
alter table front_menu_table modify id long auto_increment;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69870632

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档