[英]Read from datastore not consistent
我是Google App引擎的新手,我嘗試了一些示例代碼,但遇到了:(
下面是代碼:
datastore = DatastoreServiceFactory.getDatastoreService();
Transaction txn = datastore.beginTransaction();
Entity oSet = new Entity("Set", "Set1");
datastore.put(oSet);
Entity oItem1 = new Entity("item", "item1", oSet.getKey());
oItem1.setProperty("qty", "two");
datastore.put(oItem1);
Entity oItem2 = new Entity("item", "item2", oSet.getKey());
oItem2.setProperty("qty", "five");
datastore.put(oItem2);
Query query = new Query("item").setAncestor(oSet.getKey());
List<Entity> oItems = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(50));
for(Entity i : oItems) {
System.out.println("item qty: " + i.getProperty("qty"));
}
txn.commit();
我正在嘗試創建一個屬性為“ qty”的兩個“項目”實體。 這兩個“ item”實體是實體“ Set”的后代。 但是我無法檢索到“項目”實體。 查詢有問題嗎?
您需要放入txn.commit(); 就在datastore.put(oItem2);之后 這將確保您的寫入操作已完成,在該運行中的查詢(帶有或不帶有單獨的事務)之后,您將獲得正確的結果
希望能幫助到你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.