gpt4 book ai didi

enums - 使用 EBean 在数据库中存储枚举名称,而不是值

转载 作者:行者123 更新时间:2023-12-04 10:54:55 29 4
gpt4 key购买 nike

我有这个枚举:

public enum DocumentTypes {
PDF("PDF Document"), JPG("Image files (JPG)"), DOC("Microsoft Word documents");

private final String displayName;

DocumentTypes(final String display) {
this.displayName = display;
}

@Override
public String toString() {
return this.displayName;
}
}
和这样的模型:
@Entity
@Table(name = "documents")
public class Document extends Model {
@Id
public Long id;

@Constraints.Required
@Formats.NonEmpty
@Enumerated(EnumType.STRING)
@Column(length=20, nullable=false)
public DocumentTypes type;

@Constraints.Required
@Formats.NonEmpty
@Column(nullable=false)
public String document;
}
我在我的 Controller 中使用这个匹配枚举:
DynamicForm form = form().bindFromRequest();
// ...
Document doc = new Document();
doc.type = DocumentTypes.valueOf(form.field("type").value());
doc.save();
问题是在数据库中,它存储为“Microsoft Word 文档”,但我更愿意将其存储为 DOC。
我怎样才能做到这一点?

最佳答案

您可以使用注释 EnumMapping 将其定义为非常精细的粒度。或 EnumValue .这适用于旧版本 org.avaje.ebean。

似乎对代码进行了完全重写。在实际版本中有一个不同的 approach .

关于enums - 使用 EBean 在数据库中存储枚举名称,而不是值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11541402/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com