Unknown
Unknown

Reputation: 189

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?

Upvotes: 4

Views: 5471

Answers (1)

Sébastien Helbert
Sébastien Helbert

Reputation: 2210

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

Upvotes: 1

Related Questions