4

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?

Unknown
  • 189
  • 4
  • 18

1 Answers1

1

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

Sébastien Helbert
  • 2,185
  • 13
  • 22
  • But why does this work on all other tables except this? – Unknown Jul 01 '20 at 10:32
  • @Unknown to answer this you should show your "other" tables/entities. There might be no `@Id` annotation on `id` property or this column has no primary key constraint and can accept nulls. Or smth else – Nikolai Shevchenko Jul 01 '20 at 12:30
  • Also what are the columns types in the database ? – Sébastien Helbert Jul 01 '20 at 13:16
  • Late for this comment but hope it helps others. If the table was created before you updated the Entity, you will have to drop your Entity and have Hibernate create for you with GenerationType.IDENTITY. I was having the same problem but realised the table was created prior to my Entity and it did not have the Identity property set. So was failing one particular table. When I dropped the table and have Hibernate create. it worked fine. – sunpack Mar 08 '23 at 17:00