java Spring JPA - 枚举中枚举字段的默认值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30707621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring JPA - default value for enum field in enum
提问by Ayelet
We have an entity with an enum field - emailCommunicationStatus
, and we want to set a default value for it using JPA annotations - 'UNKNOWN'
.
我们有一个带有枚举字段 - 的实体emailCommunicationStatus
,我们想使用 JPA 注释为它设置一个默认值 - '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.
但是,当我们将实体保存到 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,
INVALID,
DONT_CONTACT,
UNKNOWN;
}
If we instead use: @Column(length = 32, columnDefinition = "varchar(32) default 'UNKNOWN'")
for emailCommunicationStatus
we get the following exception on save:
如果我们改为使用:@Column(length = 32, columnDefinition = "varchar(32) default 'UNKNOWN'")
因为emailCommunicationStatus
我们在保存时得到以下异常:
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'emailCommunicationStatus' cannot be null
What are we doing wrong? Why does it only work booleans?
我们做错了什么?为什么它只适用于布尔值?
回答by JB Nizet
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:
当某些 SQL 代码在没有为 emailCommunicationStatus 列指定任何值的情况下插入一行时,您所做的很有用。在这种情况下,该行将具有“UNKNOWN”作为该列的值:
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:
但是 Hibernate 永远不会做这样的插入。它将始终传递emailCommunicationStatus
该实体的字段的实际值。因此,如果您将其保留为 null,它将在数据库中明确地将其设置为 null:
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.
它工作正常,isLocked
因为布尔字段的默认值为 false。
回答by Robin Jonsson
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.
只需初始化实体字段。Hibernate 将使用已经设置的值实例化实体类。当它刷新时,该字段将插入指定的值。
private CommunicationStatus emailCommunicationStatus = CommunicationStatus.UNKNOWN;
Regards
问候
回答by Master Slave
Along with adding a column definitio you can set dynamic queries. But do check the comments below as well, as the approach seems to be discouaraged.
除了添加列定义,您还可以设置动态查询。但是也请检查下面的评论,因为这种方法似乎被劝阻了。
If you're using hibernate version < 4 you should annotate your entity with
如果您使用的是休眠版本 < 4,则应使用以下注释对实体进行注释
@org.hibernate.annotations.Entity(dynamicInsert = true, dynamicUpdate = true)
When the dynamic-insert property is set to true , Hibernate does not include null values for properties (for properties that aren't set by the application) during an INSERT operation. With the dynamic-update property set to true, Hibernate does not include unmodified properties in the UPDATE operation.
当 dynamic-insert 属性设置为 true 时,Hibernate 在 INSERT 操作期间不包括属性的空值(对于不是由应用程序设置的属性)。将 dynamic-update 属性设置为 true 后,Hibernate 不会在 UPDATE 操作中包含未修改的属性。
For hibernate version > 4 you should use @DynamicUpdate
instead. Also initializing in java code as well is a good advice.
对于休眠版本 > 4,您应该@DynamicUpdate
改用。同样在java代码中初始化也是一个很好的建议。