【问题标题】:Google App Engine datastore - EnumsGoogle App Engine 数据存储区 - 枚举
【发布时间】:2013-12-03 20:04:50
【问题描述】:

我正在尝试使用 JPA 将枚举保存并查询到 Google App Engine 数据存储中。
根据DataNucleas,默认情况下,枚举是 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);
    }
}

我在这里做错了什么?我真的搜索了网络和谷歌的文档,但找不到任何关于枚举的信息。你能帮忙吗?我真的卡在这里了。

非常感谢。我真的很感激。

【问题讨论】:

  • 有一个公共测试显示在 JPA 中使用 GAE 中的枚举,一切正常。建议你调试你的代码,看看你在做什么不同。 code.google.com/p/datanucleus-appengine/source/browse/trunk/…
  • @DataNucleus 感谢您的回复。有没有办法使用低级 API 持久化枚举(或者可能模拟 JPA 的方式)?我上面写的方法也可以从 servlet 调用,我无法访问 EntityManager 实例。
  • 你没有说你使用的是什么版本的谷歌 JPA 插件,以及为什么你指的是 DataNucleus 文档的 v2.2 我不知道(没有任何版本的谷歌插件适用于那)。您省略了堆栈跟踪的其余部分,这将提供有关其来源的有用信息。还要看日志
  • @DataNucleus 我正在使用 JPA 插件 v2。堆栈跟踪的其余部分与描述我的应用程序代码的问题无关。我在上面的方法中添加了 2 行,但我忘记了,希望能稍微澄清一下(实体扩展了 PropertyContainer,这是堆栈跟踪中的最后一行)。从你的声音来看,我知道这是一个假设与枚举一起使用的常见操作?通过低级 API 持久化枚举属性?
  • @DataNucleus 愿意分享您对此的看法吗?

标签: google-app-engine jpa enums google-cloud-datastore


【解决方案1】:

刚刚在我自己的代码中解决了这个问题。您正在尝试将值保存为枚举,但您应该保存枚举的字符串“名称”。尝试使用actionLogEntity.setProperty("actiontype", ACTION_TYPE.ACTION_1.name());

【讨论】:

    猜你喜欢
    • 2011-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多