我试图使用JPA将枚举保存并查询到Google App Engine数据存储中
根据DataNucleas的说法,Enum默认情况下是JPA持久性数据类型。但我得到的是以下例外:
com.xxx.utils.ActionLogUtils logCreateUserAction: actiontype: **com.xxx.endpoints.ActionLog$ACTION_TYPE** is not a supported property type.
java.lang.IllegalArgumentException: actiontype: **com.xxx.endpoints.ActionLog$ACTION_TYPE** is not a supported property type.
at com.google.appengine.api.datastore.DataTypeUtils.checkSupportedSingleValue(DataTypeUtils.java:235)
at com.google.appengine.api.datastore.DataTypeUtils.checkSupportedValue(DataTypeUtils.java:207)
at com.google.appengine.api.datastore.DataTypeUtils.checkSupportedValue(DataTypeUtils.java:173)
at com.google.appengine.api.datastore.DataTypeUtils.checkSupportedValue(DataTypeUtils.java:148)
at com.google.appengine.api.datastore.PropertyContainer.setProperty(PropertyContainer.java:101)
我的实体类如下:
@Entity
public class ActionLog {
public static enum ACTION_TYPE {
ACTION_1(1),
ACTION_2(2),
ACTION_3(3);
private final int value;
private ACTION_TYPE(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
private ACTION_TYPE actiontype;
public ActionLog() {
}
public ACTION_TYPE getActiontype() {
return actiontype;
}
public void setActiontype(ACTION_TYPE actiontype) {
this.actiontype = actiontype;
}
}
导致异常的代码是:
public static void logCreateUserAction(String userId) {
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
try {
Key userKey = KeyFactory.createKey("User", userId);
Entity actionLogEntity = new Entity("ActionLog", userKey);
actionLogEntity.setProperty("actiontype", ACTION_TYPE.ACTION_1);
datastore.put(actionLogEntity);
} catch (Exception e) {
log.log(Level.SEVERE, e.getMessage(), e);
}
}
我在这里做错了什么?我真的搜索了网络和谷歌的文档,但找不到任何关于枚举的信息。你能帮忙吗?我真的被卡住了。
非常感谢。我真的很感激。
刚刚在我自己的代码中修复了这个问题。您正试图将该值保存为枚举,但您应该保存字符串";name";枚举的。尝试使用actionLogEntity.setProperty("actiontype", ACTION_TYPE.ACTION_1.name());