Skip to content
Advertisement

Error while saving user to h2 using springboot

I’m trying to register a user entity through an API and add it to “users” table in my local dB. I keep getting the error mentioned below. I am sending a POST request with the following body:

JavaScript

It states the “NULL not allowed for column “ID”” but I don’t understand why ID is getting a null value. The User class below has @ID on email attribute, and I verified that the value being passed is not null. If anyone can point me in the right direction, and help me understand where I am going wrong, I’d truly appreciate it.

JavaScript

User.java

JavaScript

UserRepository.java

JavaScript

QuizService.java

JavaScript

QuizController.java

JavaScript

application.properties:

JavaScript

Advertisement

Answer

Hibernate had configuration spring.jpa.hibernate.ddl-auto=update set.

The original version of the entity contained the column id, so spring created a table having this column.

Later the id column was removed from the entity. Unfortunately, Hibernate can’t track removal of columns. Thus, the removed column was still present inside the database. Any consecutive insert via JPA was causing the null constraint violation of the id column.

Here is a good answer on how ddl-auto=update works:

In very simple cases like adding a column, for example, Hibernate will work very well because it’s a non-breaking change. On the other hand, if you want to delete a column, for example, it becomes more delicate. In this case, your java entity will not know about this column anymore, but in the database, it will still exist.” Seems JPA spring.jpa.hibernate.ddl-auto=update doesn’t work

Advertisement