Using hibernate for data access.
I have a column in database:
varchar(40), default value is set to 0, NOT NULL
I have a case, that user sends null value, and getting error:
Error: org.hibernate.exception.ConstraintViolationException: Cannot insert the value
NULL into column 'foo', table 'MyTable'; column does not allow nulls. INSERT fails.
And the column in hibernate is defined like this:
@Column(name = "foo")
private String foo;
Where could the problem be? Must I define the default in hibernate annotations or smthing? How? Or any other suggetsions?
What did you expect would happen when trying to insert
NULLinto aNOT NULLcolumn? Either you want to enforce aNOT NULLconstraint, and then you need to reject the user input, or you want to acceptNULLvalues and need to specify that the column is nullable using, of course,@Column(nullable = true).Since you actually want a default value in the column when the user doesn’t provide that field, and your code explicitely sets the field value even when it’s null (or empty, which is the same for Oracle, for example), I suggest having a smarter setter on the field: