【问题标题】:Store enum name, not value in database using EBean使用 EBean 在数据库中存储枚举名称,而不是值
【发布时间】:2012-07-18 12:30:44
【问题描述】:

我有这个枚举:

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;
}

我在我的控制器中使用这个匹配枚举:

DynamicForm form = form().bindFromRequest();
// ...
Document doc = new Document();
doc.type = DocumentTypes.valueOf(form.field("type").value());
doc.save();

问题是在数据库中,它存储为“Microsoft Word 文档”,但我更愿意将其存储为 DOC。

我该怎么做?

【问题讨论】:

    标签: enums playframework-2.0 ebean


    【解决方案1】:

    您可以使用注释EnumMappingEnumValue 对其进行非常精细的定义。这适用于旧版本 org.avaje.ebean。

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

    【讨论】:

    • 两个链接现在都失效了,你能添加一些描述吗?
    猜你喜欢
    • 2019-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-05
    • 2010-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多