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!
Idealny! EnumValue pracował dla mnie bardzo dobrze! Dzięki :) –
Oba linki są już martwe, czy możesz dodać do tego jakiś opis? – TheChetan