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

On Aug 8, 11:52 am, Saqib Ali <[email protected]> wrote:
> yup Java/JDO. Thanks in advance! :)
>
> On Aug 8, 11:17 am, Stephen Johnson <[email protected]> wrote:
>
> > Hi Saqib,
> > It depends on what your using. If your using Java/JDO, then I can
> > write something up for you.
> > Stephen
>
> > On Aug 8, 10:50 am, Saqib Ali <[email protected]> wrote:
>
> > > 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
>
> > > On Aug 8, 10:35 am, Stephen Johnson <[email protected]> wrote:
>
> > > > 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
>
> > > > On Aug 8, 9:04 am, Saqib Ali <[email protected]> wrote:
>
> > > > > How do I check if an entity (object) exist in the datastore? I am
> > > > > using an application generated Unencoded String for the key.......

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en.

Reply via email to