简体   繁体   English

覆盖Google App Engine- Java中的数据存储区实体

[英]Overwrite Datastore entities in Google App Engine- Java

I have an application in which I want to overwrite an individual entity. 我有一个要覆盖单个实体的应用程序。 This is how I originally create entity logs: 这是我最初创建实体日志的方式:

Entity log = new Entity("Log", "Logkey");
    String property1 = req.getParameter("property1");
    String property2 = req.getParameter("property2");
    log.setProperty("property1", property1);
    log.setProperty("property2", property2);
    datastore.put(log);

Here is how entity logs are retrieved to be overwritten: 以下是检索实体日志以进行覆盖的方法:

Query query = new Query("Log", "Logkey")
             .setFilter(timeStampFilter);
List<Entity> logs = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(1));  
request.setAttribute("logs", logs);

and sent to a jsp form page as value="${log.properties.property1}" where they should be overwritten. 并以value="${log.properties.property1}"形式发送到jsp表单页面,该页面应被覆盖。 This entry is then sent to a second servlet with the POST method and requested as parameters as in the earlier code but saved as a new entity with the same kind: 然后,使用POST方法将该条目发送到第二个servlet,并像前面的代码中一样将其作为参数进行请求,但保存为具有相同种类的新实体:

Entity edit_log = new Entity("Log", "Logkey");
        String property1 = req.getParameter("property1");
        String property2 = req.getParameter("property2");
        edit_log.setProperty("property1", property1);

For rewriting and existing entity, after retreiving a specific log by timestamp, you can get the key of this log using getKey() method and then create an entity with this key and the new details. 对于重写和现有实体,在通过时间戳检索特定日志之后,可以使用getKey()方法获取此日志的键,然后使用该键和新的详细信息创建一个实体。 Now when you put this new entity to the datastore it will replace the earlier one with the same key 现在,当您将此新实体放入数据存储区时,它将用相同的密钥替换较早的实体

With the code you've written, you only have a single Log entity in your datastore with the key "Logkey" that you are constantly overwriting. 使用您编写的代码,您的数据存储区中只有一个Log实体,并且键“ Logkey”不断被覆盖。

If you're using some other code to retrieve entities and rewrite them, then you'll need to show that other code. 如果您使用其他代码来检索实体并重写它们,则需要显示其他代码。 Otherwise, this question is poorly written, because the code given is already describing what you want to do (always overwrite the same entity). 否则,由于给出的代码已经在描述您要执行的操作(总是覆盖相同的实体),因此该问题的编写效果很差。

If you have code elsewhere creating/saving entities, it's best to show that too. 如果您在其他地方有创建/保存实体的代码,则最好也进行说明。

Edit : It looks like you end up creating a nested entity with the data from the old entity in a new entity with the same key. 编辑 :看起来您最终用相同的键在新实体中用旧实体中的数据创建了一个嵌套实体。 It's far easier just to reuse the entity you received from the query. 重用您从查询中收到的实体要容易得多。

log = logs.get(0)
log.setProperty("property1", req.getParameter("property1");
log.setProperty("property2", req.getParameter("property2");
datastore.put(log);

Furthermore, since you actually know the key ("Logkey"), you don't need to issue a datastore query, you can just fetch the entity by key - which is good because you get around eventual-consistency behavior. 此外,由于您实际上知道键(“ Logkey”),因此不需要发出数据存储查询,您只需按键就可以获取实体-这很好,因为您可以避免最终的一致性行为。

如果新实体的密钥与原始实体的密钥相同,则在存储它时它将覆盖旧实体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM