简体   繁体   中英

Cannot insert the value Null into column “ID” - Hibernate

I have created a table and its api which I am verifying through postman. Here is my primary key of the the table:

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

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

When I verify this api using Postman it gives me this error:

  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.

I have created many other tables and their apis using this same way and they are working fine. But only this table is giving me this error. Any idea why am I getting this error?

The behavior of @GeneratedValue(strategy = GenerationType.AUTO) depends on your underlying database. It seems that MSSQL doesn't support this strategy.

Try with @GeneratedValue(strategy = GenerationType.IDENTITY) instead.

See JPA GenerationType.AUTO not considering column with auto increment

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM