Spring JPA - default value for enum field in enum
Solution 1
What you did is useful whe some SQL code inserts a row without specifying any value for the emailCommunicationStatus column. In that case, the row will have 'UNKNOWN' as value for this column:
insert into account (id, isLocked) values(1, false)
But Hibernate will never do such an insert. It will always pass the actual value of the emailCommunicationStatus
field of this entity. So, if you leave it to null, it will explicitely set it to null in the database:
insert into account (id, isLocked, emailCommunicationStatus) values(1, false, null)
What you want is to set the default value of this field:
@Column(length = 32, columnDefinition = "varchar(32) default 'UNKNOWN'")
@Enumerated(value = EnumType.STRING)
private CommunicationStatus emailCommunicationStatus = CommunicationStatus.UNKNOWN;
It works fine with isLocked
because the default value of a boolean field is false.
Solution 2
Simply initialize the Entity field. Hibernate will instantiate the entity class with the value already set. When it flushes, the field will be inserted with the value specified.
private CommunicationStatus emailCommunicationStatus = CommunicationStatus.UNKNOWN;
Regards
Ayelet
Updated on November 24, 2020Comments
-
Ayelet over 3 years
We have an entity with an enum field -
emailCommunicationStatus
, and we want to set a default value for it using JPA annotations -'UNKNOWN'
.However, when we save the entity to the DB, the value of this field is
null
and not . For the boolean field -isLocked
the correct default value (false
) is saved.@Entity public class Account { @Id @GeneratedValue @Column(name = "id") protected Long id; @Column(columnDefinition = "boolean default false") private boolean isLocked; @Column(length = 32, columnDefinition = "varchar(32) default 'UNKNOWN'") @Enumerated(value = EnumType.STRING) private CommunicationStatus emailCommunicationStatus; PlayerAccount() { super(); } } public enum CommunicationStatus { VALID, INVALID, DONT_CONTACT, UNKNOWN; }
If we instead use:
@Column(length = 32, columnDefinition = "varchar(32) default 'UNKNOWN'")
foremailCommunicationStatus
we get the following exception on save:com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'emailCommunicationStatus' cannot be null
What are we doing wrong? Why does it only work booleans?
-
Robin Jonsson almost 9 yearsare you sure this works? It looks like OP's table has a constraint not allowing null values. If you INSERT something and still does not explicitly give the column a value, it will still be null.
-
Master Slave almost 9 yearsMaybe I should rephrase, I mean using column defintion in conjuction with setting the dynamic queries, will edit an answer
-
JB Nizet almost 9 yearsThat's a bad solution: now the database will have 'UNKNOWN' as the value, but the entity in memory will still have null. The Hibernate documentation discourages the usage of dynamic inserts and updates.
-
Master Slave almost 9 yearsI'm advising on setting in the code as well, maybe an overhead, since you're right about setting in code will suffice, still moving some attention to dynamic update construct is not bad in my opinion
-
Master Slave almost 9 years
The Hibernate documentation discourages the usage of dynamic inserts and updates.
This I wasn't aware of, I actually tought it was a recommended way as the construct is meant just for the case. I'll leave the answer, and edit it with a bad practice comment -
chill appreciator over 4 yearsThis method has a drawback. If you take a look at the table SQL definition you won't see any default value in it. So, if you override them with null, the entity will be saved without any error. more details