views:

346

answers:

2

On GAE with Spring/JDO after saving 2 entities (in transaction).
On calling getById - entities fetched from data storage.
On calling getCount() returns "0"
and - on calling getAll() - returns empty collection.

 @Override
 public Long getCount() {
  return ((Integer) getJdoTemplate().execute(new JdoCallback() {
   @Override
   public Object doInJdo(PersistenceManager pm) throws JDOException {
    Query q = pm.newQuery(getPersistentClass());
    q.setResult("count(this)");
    return q.execute();
   }
  })).longValue();
 }

 @Override
 public void saveOrUpdate(T entity) {
  getJdoTemplate().makePersistent(entity);
 }

 @Override
 public List<T> getAll() {
  return new ArrayList<T>(getJdoTemplate().find(getPersistentClass()));
 }
A: 

Google's implementation of JDO does not currently support aggregates AFAIK. Try keeping track of the count by updating some other entity every time you persist a new entity. If you are doing frequent writes, you'll want a "sharded" counter.

Your question is pretty close to this one, so reading those answers may help.

Peter Recore
A: 

count() is actually implemented in GAE/J's plugin, as seen here http://code.google.com/p/datanucleus-appengine/source/browse/trunk/src/org/datanucleus/store/appengine/query/DatastoreQuery.java#341

If you have a problem with it then suggest that you provide a testcase to Google and raise an issue on their issue tracker for their GAE/J DN plugin ("Issues" on the linked page)

DataNucleus