Home > database >  Hibernate inserts null for ID when using strategy = GenerationType.IDENTITY
Hibernate inserts null for ID when using strategy = GenerationType.IDENTITY

Time:02-28

Why is Hibernate inserts null value for the ID in the table.

@Id
@Getter
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

This is the Message I am getting when inserting somthing in table blabla ↓↓↓

Hibernate: insert into blabla (id, bla1, bla2) values (null, ?, ?)

However when instad of using strategy = GenerationType.IDENTITYstrategy = GenerationType.AUTO/SEQUANSE I am getting this message ↓↓↓

Hibernate: call next value for hibernate_sequence

Hibernate: insert into blabla (bla1, bla2, id) values (?, ?, ?)

Does anyone know why it's like that because this null in ID place is causing an Exception when connecting to a database.

Update:

I've run my Application with strategy = SEQUENCE and AUTO but I am getting also an Exception

Database is H2

Solution:

you go to your application.properties file and add this line of code:

hibernate.hbm2ddl.auto=validate

CodePudding user response:

I think you have the same issue with this one. https://forum.hibernate.org/viewtopic.php?t=985693

Maybe your table hasn't been defined as an auto incremental PK.

  • Related