2012-07-18 13 views
6

mam ten ENUM:Store nazwa enum, a nie wartość w bazie danych przy użyciu EBean

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

i model takiego:

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

dopasować enum przy użyciu tego w moim kontrolera:

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

Problem polega na tym, że w bazie danych jest on przechowywany jako "dokumenty Microsoft Word", ale wolałbym przechowywać go jako DOC.

Jak mogę to zrobić?

Dzięki za pomoc!

Odpowiedz

10

Możesz zdefiniować to bardzo dobrze ziarnisto z Anotation EnumMapping lub EnumValue. Działa to ze starszą wersją org.avaje.ebean.

Wygląda na to, że doszło do pełnego przepisania kodu. W aktualnej wersji jest inny approach.

+0

Idealny! EnumValue pracował dla mnie bardzo dobrze! Dzięki :) –

+0

Oba linki są już martwe, czy możesz dodać do tego jakiś opis? – TheChetan

Powiązane problemy