[英]How to perform bulk update on AppEngine Datastore
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. sale
是我要添加到实体种类中的全新领域。 So it does not exist yet. 因此它尚不存在。
UPDATE 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);
There is an error in your code. 您的代码中有错误。 You never update
entities
. 您永远不会更新
entities
。 It should be: 它应该是:
Query q = new Query("Product");
List<Entity> entities = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
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. 由于某些原因,可迭代的
entities
行为不像适当的Java集合。 In a Java collection, the elements are pointers. 在Java集合中,元素是指针。 But for whatever reason, here each entity that you get inside the for-loop is an independent deep copy.
但是无论出于何种原因,这里您进入for循环的每个实体都是一个独立的深层副本。 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.