check for existence of a entity (object) in the datastore?

736 views
Skip to first unread message

Saqib Ali

unread,
Aug 8, 2010, 12:04:49 PM8/8/10
to Google App Engine
How do I check if an entity (object) exist in the datastore? I am
using an application generated Unencoded String for the key.......


Stephen Johnson

unread,
Aug 8, 2010, 1:35:01 PM8/8/10
to Google App Engine
If your using Java you can create an encoded key by using the
KeyFactory class (I don't know the equivalent in Python) and then
create a query to just query for the key field (not the entire entity)
and see if you get any results back. You want to just query for the
key field alone so that it is as fast as possible. I don't know if
there is any other way than actually performing a query.

Stephen

Saqib Ali

unread,
Aug 8, 2010, 1:50:10 PM8/8/10
to Google App Engine
Stephen,

Thanks for the response. Do you have some sample code for this? I have
trying to do exactly this, but can't seem to make it work. I am new to
datastore......

saqib

Stephen Johnson

unread,
Aug 8, 2010, 2:17:36 PM8/8/10
to Google App Engine
Hi Saqib,
It depends on what your using. If your using Java/JDO, then I can
write something up for you.
Stephen

Saqib Ali

unread,
Aug 8, 2010, 2:52:48 PM8/8/10
to Google App Engine
yup Java/JDO. Thanks in advance! :)

Stephen Johnson

unread,
Aug 8, 2010, 3:41:07 PM8/8/10
to Google App Engine
It took me a little bit to figure out what was going on because in the
datastore the key property is __key__ so if you query using the
Datastore Viewer you would do something like:

select __key__ from Something

but when using JDO you've got to use the attribute name of the key
field and JDO will map it to __key__

Ok, so what I've done is to have all my datastore classes inherit from
my own abstract Entity class where I've defined the following:

@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
@Extension(vendorName="datanucleus", key="gae.encoded-pk",
value="true")
private String ek; // some use encodedKey for this name, but I
keep them short to save datastore space

@Persistent
@Extension(vendorName="datanucleus", key="gae.pk-name",
value="true")
private String id;

Then I wrote this utility method which works for any class that I want
to check to see if a key already exists:

public static boolean doesEntityExist(Class<? extends Entity>
entityClass, String id) {
PersistenceManager pm = null;
try {
pm = PMF.getPersistenceManager();
String idKeyString =
KeyFactory.createKeyString(entityClass.getSimpleName(), id);
Query q = pm.newQuery("select ek from " + entityClass.getName()
+
" where ek = '" + idKeyString + "'");
List<?> keys = (List<?>)q.execute();
return keys.size() > 0;
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (pm != null) pm.close();
}
}

So, if this is in a class called Utility and you have an entity class
Employee and you want to check for an unencoded key string of
"EMP-1234" just call it like this:

if (Utility.doesEntityExist(Employee.class, "EMP-1234")) {
// entity with key already exists
}

Let me know if this helps,
Stephen

Stephen Johnson

unread,
Aug 8, 2010, 3:45:28 PM8/8/10
to Google App Engine
One more thing. This only works if the entity doesn't have parents/
ancestors. I don't use that part of the JDO and manage all my
relationships myself that's why I can build the key the way I do. If
the entity has ancestors that you've got to build the key differently.

Saqib Ali

unread,
Aug 8, 2010, 5:55:08 PM8/8/10
to Google App Engine
@Stephen,

Thank you very much. This looks promising. I will let you know how it
goes.

Saqib

On Aug 8, 12:41 pm, Stephen Johnson <onepagewo...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages