-4

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"; }
João Dias
  • 16,277
  • 6
  • 33
  • 45
AZetZ
  • 21
  • 3

2 Answers2

0

This will work for you.

@Column(nullable = false, columnDefinition = "varchar(16)")
 private String role = "USER";
vaibhavsahu
  • 612
  • 2
  • 7
  • 19
  • Thanks, of course it works. but there are some differences between declaration inside annotation and in default value of field without annotation. My question was rather what is the reason of error. Maybe there is any difference on compilation step between 2 ways of declaration. The 3rd way is also solution mentioned by me with @PrePersist. – AZetZ Aug 21 '21 at 23:45
0

If I am not mistaken columnDefinition = "varchar(16) default 'USER'" already guarantees that you will never have a null role in the database because it affects SQL table definition.

Having said that I would suggest removing nullable = false from the @Column annotation in the field.

João Dias
  • 16,277
  • 6
  • 33
  • 45
  • Great! I didn't think that it is in opposite - default value vs. nullable=false – AZetZ Aug 22 '21 at 12:15
  • I can add after tests that "default = 'USER'" is not working at least in current version of Spring boot/JPA/Hibernate(/...?). DEFAULT value is added into MariaDB, but in java code it does not work. So @vaibhavsahu solution (private String role = "USER") could be the simplest way to close this problem or maybe alternatively the condition inside PrePersist. Thanks both again – AZetZ Aug 22 '21 at 12:45
  • Yep, @vaibhavsahu suggestion is in fact the simplest way, but your implementation should work also (reference https://www.baeldung.com/jpa-default-column-values). – João Dias Aug 22 '21 at 14:29