title: How to I determine if an object exists for a given key in the Google AppEngine datastore using Java?
body: <p>I'm trying to port the Sharding Counters example (code.google.com/appengine/articles/sharding_counters.html) to Java. The only problem is that the Java API does not have a call similar to Python's 'get_by_key_name'. This is the basic idea:</p>
  
  <pre><code>            Transaction tx = pm.currentTransaction();
              Key key = KeyFactory.createKey(CounterShard.class.getSimpleName(), counter + randomIndex);
              CounterShard shard = pm.getObjectById(CounterShard.class, key);
              if (shard == null) {  // API does not work this way...
                  shard = new CounterShard(key, counter);
              }
              shard.increment();
              pm.makePersistent(shard);
              tx.commit();
  </code></pre>
  
  <p>Unfortunately, this throws a JDOObjectNotFoundException the first time I run it. I could run a query to determine if a counter for a given name exists, but that is not transactional. Another thread could do the same and in the end both would create an object with the same key.</p>
  
  <p>From what I understand, the only operations supported within a transaction (for the Java API) are get and put. So how can I lock an object by key that does not exist yet (i.e. no 'get') and make sure that I am the first and only one creating it?</p>