Apr 25, 2013

GAE - PreparedQuery.TooManyResultsException

Just take note before using asSingleEntity you must to make sure that you query will on return one entity(record) or else you will came up a problem TooManyResultsException

Suggestion to limit this unexpected behavior

public static Entity getSingleData(Query q){
  FetchOptions fOption = FetchOptions.Builder.withLimit(1);
  try{
   return datastore.prepare(q).asList(fOption).get(0);
  }catch (IndexOutOfBoundsException e) {
   logger.warning("No data sorry: "+e.getMessage());
   return null;
   
  }
  //return datastore.prepare(q).asSingleEntity();
 }