Spring JPA - 枚举中枚举字段的默认值 [英] Spring JPA - default value for enum field in enum
问题描述
我们有一个具有枚举字段的实体 - emailCommunicationStatus
,我们要使用JPA注释设置一个默认值 - 'UNKNOWN'
。
但是,当我们将实体保存到DB时,该字段的值为 null
而不是。对于布尔字段 - isLocked
保存正确的默认值( false
)。
@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,
无效,
DONT_CONTACT,
UNKNOWN;
}
如果我们改用: @Column(length = 32,columnDefinition =varchar(32)default'UNKNOWN')
for emailCommunicationStatus
我们在保存时收到以下异常:
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:列'emailCommunicationStatus'不能为null
/ pre>
我们做错了什么?为什么它只能工作布尔?
解决方案当某些SQL代码插入一行但没有指定任何值emailCommunicationStatus列。在这种情况下,该行将具有UNKNOWN作为此列的值:
insert into account(id,isLocked)values (1,false)
但Hibernate永远不会这样插入。它将始终通过该实体的
emailCommunicationStatus
字段的实际值。所以,如果你把它留空,它会在数据库中显式设置为null:insert into account(id, isLocked,emailCommunicationStatus)value(1,false,null)
你想要的是设置默认值的字段:
@Column(length = 32,columnDefinition =varchar(32)default'UNKNOWN')
@Enumerated(value = EnumType.STRING)
private CommunicationStatus emailCommunicationStatus = CommunicationStatus.UNKNOWN;
由于默认值为
isLocked
一个布尔域的值为false。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?
解决方案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.这篇关于Spring JPA - 枚举中枚举字段的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!