Search code examples
javagoogle-app-enginejdo

Error while getting Entity using getObjectById : javax.jdo.JDOObjectNotFoundException: No such object


I am using JDO on app engine. For some reason the method getObjectById of PersistenceManager returns:

No such object
org.datanucleus.exceptions.NucleusObjectNotFoundException: No such object

code:

public Image getById(long id) {
        Image img = null;
        PersistenceManager pm = PMF.get().getPersistenceManager();
        try {
            img = (Image) pm.getObjectById(id);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            pm.close();
        }
        return img;
    }

In other part of this file I am able to successfully retrieve all Entities using Query:

 public List<Image> getAll() {
        PersistenceManager pm = PMF.get().getPersistenceManager();

        List<Image> results = null;
        Query q = pm.newQuery(Image.class);
        q.setOrdering("date desc");
        try {
            results = (List<Image>) q.execute();
        } finally {
            q.closeAll();
        }
        return results;
    }

Debugging: enter image description here enter image description here

It's shown/obvious that datastore holds the object with requested id. Why this exception is being thrown?


Solution

  • You need to specify the class of the object that you are trying to retrieve.

    So, the typical signature of that method should be something like:

    getObjectById(Class arg0, Object arg1);

    where arg0 is the class that you are trying to retrieve i.e. Image.class and arg1 is the key value object.