gpt4 book ai didi

java - 无法将值 Null 插入列 "ID"- hibernate

转载 作者:行者123 更新时间:2023-12-05 06:14:55 26 4
gpt4 key购买 nike

我已经创建了一个表及其 api,我正在通过 postman 进行验证。这是我的表的主键:

 @Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "ALERT")
public class Table Name{

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ALERTID")
private Integer id;

当我使用 Postman 验证这个 api 时,它给我这个错误:

  SQL [n/a]; constraint [null]; nested exception is 
org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause

com.microsoft.sqlserver.jdbc.SQLServerException: Cannot insert the value NULL
into column 'ID', table
'CRM.dbo.ALERT';
column does not allow nulls. INSERT fails.

我用同样的方法创建了许多其他表和它们的 api,它们工作正常。但只有这张表给我这个错误。知道为什么我会收到此错误吗?

最佳答案

@GeneratedValue(strategy = GenerationType.AUTO) 的行为取决于您的基础数据库。 MSSQL好像不支持这个策略。

尝试使用 @GeneratedValue(strategy = GenerationType.IDENTITY) 代替。

参见 JPA GenerationType.AUTO not considering column with auto increment

关于java - 无法将值 Null 插入列 "ID"- hibernate ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62674464/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com