代码之家  ›  专栏  ›  技术社区  ›  Carl

googleappengine:从任务中的数据存储中获取对象时出现问题

  •  3
  • Carl  · 技术社区  · 14 年前

    我的应用程序构造了一个 Parent Child ren,然后启动任务在 任,就像这样:

    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)。

    2 回复  |  直到 14 年前
        1
  •  3
  •   Carl    14 年前

    在进行了一些插入操作之后,下面的步骤将能够正确地检索所需的 Child

        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);
    

    对于为什么type+id不足以获取我想要的东西,来自非数据存储世界的我仍然有点迷惑。这似乎相当于在sqlland中知道table+主键,文档似乎表明该键包含所有父信息,因此拥有它就足以直接执行一个操作 pm.getObjectId(Child, KeyFactory./* etc */)

        2
  •  0
  •   Donato Szilagyi    11 年前
    public Object retrieveChildByKey(Key parent, Class childClass, int idChild){
        try{
            pm = PMF.get().getPersistenceManager();
            Key objectKey = KeyFactory.createKey(parent, childClass.getSimpleName(), idChild);
            Object result = pm.getObjectById(childClass, objectKey);
            return result;
        }
        catch (DatastoreTimeoutException ex) {
            // Display a timeout-specific error page
            message = "Timeout: An error has occurred: Was not possible to retrieve the Object";
            System.out.println(message);
            ex.printStackTrace();
        }
        catch(Exception ex){
            message = "Timeout: An error has occurred: Was not possible to retrieve the Object";
            System.out.println(message);
            ex.printStackTrace();
        }finally{
            pm.close();
        }
        return null;
    }