有没有办法让数据存储区为嵌入式实体分配自动ID?我使用Mutation.Builder.addInsertAutoId并且它适用于“普通”实体,但在尝试为嵌入式实体生成ID时出错:
ApplicationError:1:实体缺少密钥。
示例代码
Entity.Builder entity = Entity.newBuilder();
Key.Builder key = makeKey(m.getClass().getSimpleName());
entity.setKey(key);
entity.addProperty(makeProperty("name", makeValue(m.getName())));
Entity.Builder embeddedEntity = Entity.newBuilder();
addProperty(embeddedEntity, "name", "a different name");
entity.addProperty(makeProperty("embedded", makeValue(embeddedEntity).setIndexed(false)));
CommitRequest req = CommitRequest.newBuilder()
.setMode(CommitRequest.Mode.NON_TRANSACTIONAL)
.setMutation(
Mutation.newBuilder()
.addInsertAutoId(entity)
.addInsertAutoId(embeddedEntity)
)
.build();
堆栈跟踪
com.google.apphosting.api.ApiProxy$ApplicationException: ApplicationError: 1: Entity is missing key. at com.google.appengine.api.datastore.dev.LocalDatastoreV4Service.commit(LocalDatastoreV4Service.java:162) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.google.appengine.tools.development.ApiProxyLocalImpl$AsyncApiCall.callInternal(ApiProxyLocalImpl.java:521) at com.google.appengine.tools.development.ApiProxyLocalImpl$AsyncApiCall.call(ApiProxyLocalImpl.java:475) at com.google.appengine.tools.development.ApiProxyLocalImpl$AsyncApiCall.call(ApiProxyLocalImpl.java:452) at java.util.concurrent.Executors$PrivilegedCallable$1.run(Executors.java:533) at java.security.AccessController.doPrivileged(Native Method) at java.util.concurrent.Executors$PrivilegedCallable.call(Executors.java:530) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745)
我尝试了不同的变体,我在什么时候调用addInsertAutoId
无关紧要。
答案 0 :(得分:0)
设置' stub'密钥,如examples for App Engine所示,可以解决问题:
Entity.Builder embeddedEntity = Entity.newBuilder();
addProperty(embeddedEntity, "name", "a different name");
embeddedEntity.setKey(entity.getKey) // ← required