如何在AppEngine Datastore上执行批量更新 [英] How to perform bulk update on AppEngine Datastore

查看:92
本文介绍了如何在AppEngine Datastore上执行批量更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码无法正常工作。有人知道我可以如何工作吗?

 查询q =新的查询(产品); 
Iterable< Entity> entities = datastore.prepare(q).asIterable();
for(Entity entity:datastore.prepare(q).asIterable()){
entity.setProperty(sale,false);
}
datastore.put(entities);

sale 是一个全新的领域,我我正在添加到实体类中。所以它还没有存在。



更新



我将其修正如下但代码仍然无法正常工作

 查询q =新查询(Product); 
Iterable< Entity> entities = datastore.prepare(q).asIterable(); $(实体实体)
{
entity.setProperty(sale,false);
}
datastore.put(entities);


解决方案

也许别人可以向你解释为什么它确实不工作,但我知道如何使工作。

由于某些原因,实体 iterable的行为不像一个正确的Java集合。在Java集合中,元素是指针。但无论出于何种原因,您在for循环中获得的每个实体都是独立的深层副本。所以,相反,执行以下操作,它将工作

 查询q = new Query(Product); 
列表< Entity> products = new ArrayList< Entity>();
for(Entity entity:datastore.prepare(q).asIterable()){
entity.setProperty(sale,false);
products.add(entity);
}
datastore.put(products);


The following code is not working. Does anyone know how I might get it to work?

Query q = new Query("Product");
    Iterable<Entity> entities = datastore.prepare(q).asIterable();
    for (Entity entity : datastore.prepare(q).asIterable()) {
        entity.setProperty("sale", false);
    }
    datastore.put(entities);

sale is a completely new field that I am adding to the entity kind. So it does not exist yet.

UPDATE

I fixed it as below but the code is still not working

Query q = new Query("Product");
    Iterable<Entity> entities = datastore.prepare(q).asIterable();
    for (Entity entity : entities) {
        entity.setProperty("sale", false);
    }
    datastore.put(entities);

解决方案

Maybe someone else can explain to you why exactly it does not work, but I know how to make it work.

For some reason the entities iterable does not behave like a proper Java collection. In a Java collection, the elements are pointers. But for whatever reason, here each entity that you get inside the for-loop is an independent deep copy. So instead, do the following and it will work

    Query q = new Query("Product");
    List<Entity> products = new ArrayList<Entity>();
    for (Entity entity : datastore.prepare(q).asIterable()) {
        entity.setProperty("sale", false);
        products.add(entity);
    }
    datastore.put(products);

这篇关于如何在AppEngine Datastore上执行批量更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆