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

Cyr*_* N. 7 enums ebean playframework-2.0

我有这个枚举:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

这样的模型:

@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;
}
Run Code Online (Sandbox Code Playgroud)

我在我的控制器中使用它来匹配枚举:

DynamicForm form = form().bindFromRequest();
// ...
Document doc = new Document();
doc.type = DocumentTypes.valueOf(form.field("type").value());
doc.save();
Run Code Online (Sandbox Code Playgroud)

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

我怎样才能做到这一点 ?

谢谢你的帮助!

nie*_*els 11

您可以使用Anotation EnumMappingEnumValue定义非常精细的粒度.这适用于旧版本org.avaje.ebean.

似乎完全重写了代码.在实际版本中,有一种不同的方法.