要存储在数据库中的java枚举
我有这样一个枚举类-
public enum FeedbackStatus {
@JsonProperty("unprocessed")
UNPROCESSED("unprocessed"),
@JsonProperty("arrived")
ARRIVED("arrived"),
@JsonProperty("performed")
PERFORMED("performed"),
@JsonProperty("cancelled")
CANCELLED("cancelled"),
@JsonProperty("removed")
REMOVED("removed"),
@JsonProperty("no-show")
NO_SHOW("no-show"),
@JsonProperty("cancel-at-door")
CANCEL_AT_DOOR("cancel-at-door");
private static final FeedbackStatus[] myEnumValues = FeedbackStatus.values();
private final String fieldDescription;
private FeedbackStatus(String value) {
this.fieldDescription = value;
}
public static FeedbackStatus fromString(String string) {
if (!TextUtils.isEmpty(string)) {
for (FeedbackStatus feedbackStatus : myEnumValues) {
if (feedbackStatus.getString().equalsIgnoreCase(string)) {
return feedbackStatus;
}
}
}
throw new IllegalArgumentException("No constant with text " + string + " found");
}
public String getString() {
return this.fieldDescription;
}
}
我需要使用GreneDAO在SQLITE数据库中存储一个对象feedback
,但该对象是一个枚举,因此我无法存储它。这个物体就像——
private FeedbackStatus feedback;
如何将其存储为字符串
# 1 楼答案
您不能将
enums
存储在数据库中,因为enumns
是Java
实体,所以对于枚举,您需要存储它们的等价值String
或int
等你可以获得
value
的enum
,如下所示,然后将其存储在database
中Strings
:有关枚举的更多详细信息,请参见Java Docs here
所以这里实际上你的数据模型是可以的,但是当你在数据库中保存enum时,你只需要一个带有enum值的列,当你插入时,你需要从enum转换到它的字符串值,当你从db读取时,你需要从这个字符串构建enum
希望有帮助