Java-Google App引擎:在任务中从数据存储中获取对象时出现问题
作者:互联网
我的应用程序在静态工厂中构造一个Parent对象以及它的预定子项,然后启动任务以对子项运行一些计算,如下所示:
public static Parent make(User owner, List<Integer> data, int size) {
Parent result = new Parent(owner,data,size);
PersistenceManager pm = PersistenceSource.get();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
result = pm.makePersistent(result);
for (int i=0; i<size; pm.makePersistent(new Child(result,i++)));
pm.close();
tx.commit();
} finally {
if (tx.isActive()) { tx.rollback(); result=null; }
}
if (result!=null) {
Queue q = QueueFactory.getDefaultQueue();
for (Child c : result.getChild()) {
q.add(url("/task/child").param("key", KeyFactory.keyToString(c.getKey())).method(Method.PUT));
}
}
pm.close();
return result;
}
但是在实际任务中
public void doPut(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
PersistenceManager pm = PersistenceSource.get();
Child c = pm.getObjectById(Child.class, KeyFactory.stringToKey(request.getParameter("key"))); //...
它死于试图找到对象:
Could not retrieve entity of kind Child with key Child(24)
org.datanucleus.exceptions.NucleusObjectNotFoundException: Could not retrieve entity of kind Child with key Child(24)
有什么见解吗?同样,如果很重要,则父级-子级关系由父级定义为子级中的字段(因此,将父级作为arg进行构造).
解决方法:
进行一些插入之后,以下操作将正确检索所需的子代:
Key k = new KeyFactory
.Builder(Parent.class.getSimpleName(), Long.valueOf(request.getParameter("parent")))
.addChild(Child.class.getSimpleName(), Long.valueOf(request.getParameter("child")))
.getKey();
Child c = pm.getObjectById(Child.class, k);
对于为什么类型ID不足以获取我想要的内容,我仍然对来自非DataStore的世界感到困惑.这似乎等同于了解SQL域中的表主键,并且文档似乎表明该键包含所有父信息,因此拥有该键足以执行直接的pm.getObjectId(Child,KeyFactory./*等* /).
标签:google-app-engine,task,google-cloud-datastore,queue,java 来源: https://codeday.me/bug/20191209/2097030.html