We have an object with an enumeration field - emailCommunicationStatusand we want to set the default value for it using JPA - annotations 'UNKNOWN'.
However, when we save the object in the database, the value of this field is equal to null, but not. For a boolean field, isLockedthe correct default value ( false) is stored .
@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'")for emailCommunicationStatus, we get the following exception when saving:
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'emailCommunicationStatus' cannot be null
What are we doing wrong? Why does it work only with boolers?
source
share