I am trying to find reason why my problem exists. I have default value set up as 'USER' but hibernate says that this field cannot be null.
Error code:
*Hibernate: insert into user2 (active, role, user_id) values (?, ?, ?)
2021-08-22 00:40:28.182 WARN 154195 --- [nio-8180-exec-5] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1048, SQLState: 23000
2021-08-22 00:40:28.182 ERROR 154195 --- [nio-8180-exec-5] o.h.engine.jdbc.spi.SqlExceptionHelper : (conn=3610) Column 'role' cannot be null
2021-08-22 00:40:28.195 ERROR 154195 --- [nio-8180-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause
java.sql.SQLException: Column 'role' cannot be null*
Entity is designed within default value of field (USER):
@Column(nullable = false, columnDefinition = "varchar(16) default 'USER'")
private String role;
Inside DB also default value is present.
I found solution with @PrePersist
annotation but it is strange why I need to use such thing:
@PrePersist
private void onPrePersist() { role = "USER"; }