1

当我尝试保留二维 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);

在代码片段中 cng 的类型是:

ArrayList<ArrayList<String>>

我最初使用

ArrayList<HashMap<Byte,Boolean>>

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

4

3 回答 3

1

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

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

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

于 2013-10-07T15:23:16.743 回答
1

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

于 2013-10-07T15:24:06.227 回答
1

使用可以存储为实体属性的 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);

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

于 2016-05-06T19:51:22.727 回答