【问题标题】:Google App Engine Array List is Not a Supported Property TypeGoogle App Engine 数组列表不是受支持的属性类型
【发布时间】:2013-10-07 15:04:15
【问题描述】:

当我尝试保留二维 ArrayList 时,出现以下错误:

java.lang.IllegalArgumentException: cng_content: java.util.ArrayList is not a supported property type.

我正在使用两个数组列表构造一个矩阵的表示,并尝试将其持久化到数据存储中。

    Key cngKey = KeyFactory.createKey("CNG", jsonCNG.cNGID);
    Entity cngEntity = new Entity("CNG", cngKey);
    cngEntity.setProperty("cng_name", jsonCNG.cNGName);
    cngEntity.setProperty("cng_type", jsonCNG.cNGType);
    cngEntity.setProperty("cng_content", cng);

在代码中 sn-p cng 的类型是:

ArrayList<ArrayList<String>>

我最初使用过

ArrayList<HashMap<Byte,Boolean>>

作为对象的类型。但是,发现 GAE 数据存储不支持 HashMap。此外,我不打算查询存储的对象。只是为了存储和检索它们。

【问题讨论】:

    标签: java arrays google-app-engine collections


    【解决方案1】:

    如果您不查询,请将它们保存为 json 或其他文本格式。注意最大实体大小。

    【讨论】:

    • 这可能无关。但是,您知道如何通过 id 获取持久对象吗?我收到一个错误:com.google.appengine.api.datastore.EntityNotFoundException:未找到与密钥匹配的实体:CNG("T78"),当将密钥设置为 Key cngKey = KeyFactory.createKey("CNG", " T78")
    • 创建实体时,您使用new Entity("CNG", cngKey); 创建它,它实际上创建了一个实体,其种类为“CNG”和 cngKey 的祖先(id 将自动填充)。相反,您应该使用new Entity(cngKey) 创建您的实体。然后您可以使用datastore.get(cngKey) 查找您的对象。
    • 糟糕,看起来彼得已经在另一个问题中回答了这个问题:stackoverflow.com/questions/19233213/…
    • 如果您存储 JSON,我会考虑转换为 Text 类。因为数据存储字符串有长度限制:cloud.google.com/appengine/docs/standard/java/javadoc/com/…
    【解决方案2】:

    setProperty(name, value) 方法采用支持的 java 类型的 supported java typesCollection(这包括 ArrayList)。但是集合内的集合不是受支持的类型。

    这些被称为多值属性并且有一个用途 - 集合的每个值都有自己的索引条目,因此查询实际上可以根据集合中的值找到实体。

    在您的情况下,您最好将二维列表的一维序列化为字节数组并将其存储在 Blob 中,然后将所有 blob 存储为 List&lt;Blob&gt;

    【讨论】:

      【解决方案3】:

      使用可以作为属性存储在实体上的 EmbeddedEntity。由于您只使用 2D,因此将每个数组设置为 EmbeddedEntity 上的一个属性,其中键是一个数字,但以字符串格式表示,如“1”、“2”、“3”。

      更具体:

      Entity e = new Entity("2d");
      EmbeddedEntity ee = new EmbeddedEntity();
      
      ArrayList<String> x = new ArrayList<String>();
      // add stuff to x
      
      ArrayList<String> y = new ArrayList<String>();
      // add stuff to y
      
      ArrayList<String> z = new ArrayList<String>();
      // add stuff to z
      
      ee.setProperty("1", x);
      ee.setProperty("2", y);
      ee.setProperty("3", z);
      e.setProperty("2dArray", ee);
      

      我在没有测试的情况下输入了这个,所以可能有语法错误

      【讨论】:

        猜你喜欢
        • 2013-11-14
        • 2012-02-27
        • 2017-06-12
        • 1970-01-01
        • 1970-01-01
        • 2012-10-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多