java - 如何将 HashMap(作为对象属性)保存到数据存储区

标签 java google-app-engine google-cloud-datastore

我正在使用 GAE(Google App Engine)数据存储来存储人员。

每个人都有一个名字、一个唯一的 token 、他们收到的消息列表(字符串)和他们完成的测试列表(字符串)。这些都是supported types .

但是,我还想存储一个包含 String 键和 String 值的 HashMap,其中包含有关它们已完成的测试的信息。

我正在尝试保存的示例 map (请注意,这是示例数据,而不是实际数据)

[
    <'test_easy_1', 'completed in 3 minutes'>
    <'test_easy_2', 'completed in 5 minutes'>
    <'test_hard_1', 'completed in 15 minutes'>
    <'test_hard_2', 'completed in 3 minutes.. Did you cheat?'>
]

我正在努力挽救这样的人

@Override
public boolean savePerson(Person p) {
    if (p.getEntity() == null) {
        return false;
    }

    p.getEntity().setProperty("name", p.getName());
    p.getEntity().setProperty("token", p.getToken());
    p.getEntity().setProperty("messages", p.getMessages());
    p.getEntity().setProperty("completedTests", p.getCompletedTests());
    p.getEntity().setProperty("testInformation", p.getTestInformation()); // does not work
    DatastoreServiceFactory.getDatastoreService().put(p.getEntity());

    return true;
}

现在是我的问题:

如何将 HashMap 作为对象属性保存到数据存储区?

我希望以一种不需要为 HashMap 创建整个类和新数据存储索引的方式来执行此操作。但是,如果这是唯一的方法,我想知道如何去做。谷歌搜索并没有找到清晰明显的方法来处理这类情况。

最佳答案

尽管其他答案可能是正确的,但我主要是在寻找一种无需使用外部库即可实现此目的的方法。

我对前面提到的 EmbeddedEntity 做了一些研究发现这确实是我要找的东西。

我正在撰写并接受这个答案,因为它为我的问题提供了准确的解决方案,而不仅仅是解决问题的指南。我希望通过这种方式来指导 future 遇到类似问题的人。

保存人现在看起来像这样:

@Override
public boolean savePerson(Person p) {
    if (p.getEntity() == null) {
        return false;
    }

    p.getEntity().setProperty("name", p.getName());
    p.getEntity().setProperty("token", p.getToken());
    p.getEntity().setProperty("messages", p.getMessages());
    p.getEntity().setProperty("completedTests", p.getCompletedTests());

    EmbeddedEntity ee = new EmbeddedEntity();
    Map<String, String> testInformation = p.getTestInformation();

    for (String key : testInformation.keySet()) { // TODO: maybe there is a more efficient way of solving this
        ee.setProperty(key, testInformation.get(key));
    }

    p.getEntity().setProperty("testInformation", ee);
    DatastoreServiceFactory.getDatastoreService().put(p.getEntity());

    return true;
}

现在加载人像这样:

Person p = new Person(id);
p.setName((String) e.getProperty("name"));
p.setToken((String) e.getProperty("token"));
p.setMessages((List<String>) e.getProperty("messages"));
p.setCompletedTests((List<String>) e.getProperty("completedTests"));

Map<String, String> ti = new HashMap<>();
EmbeddedEntity ee = (EmbeddedEntity) e.getProperty("testInformation");
if (ee != null) {
    for (String key : ee.getProperties().keySet()) {
        ti.put(key, (String) ee.getProperty(key));
    }
    p.setTestInformation(ti);
}
p.setEntity(e);

关于java - 如何将 HashMap(作为对象属性)保存到数据存储区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27966765/

相关文章:

java - 在 Azure DocumentDB 上按 ID 进行查询没有响应

java - 将给定日期转换为通用日期格式?

google-app-engine - 如何使用GAE/SE go112将大文件上传到Google云端存储

java - Collectors.joining 与 StringBuilder.append

java - 如何在调整自定义 JSlider 大小时重新绘制

django - 在 Google App Engine (Django) 上使用 Beaker

java - Google App Engine 如何预编译 Java?

java - Google App Engine - 在数据存储查看器中看不到子项

django - Google App Engine 教程问题 - 新数据存储模型

python - 如何从 App Engine (Python) Datastore 中获取当月的记录?