Obviously the simple way to do it is to build a class reference using
Key.getKind().
Like

Key key = myObject.getKey();
String myPackage = "com.example.entities."
Class cl = Class.forName(myPackage + key.getKind());
pm.getObjectById(class, key);

This surely works in development, provided that entities are in a
known package.
But it's definitely horrible.

On Sep 7, 11:48 am, "l.denardo" <lorenzo.dena...@gmail.com> wrote:
> HEllo everybody,
> I'm facing an issue with JDO which is presented elsewhere in the
> group, but with no workaround.
>
> I simply need to retrieve an object from JDO using only the key.
> That is do something like this
>
> PersistenceManager pm = pmf.getPersistenceManager();
> SimpleObject ll = new SimpleObject;
> try{
>         pm.makePersistent(ll);
>         Key leftKey = ll.getKey();
>         Object retriveved = pm.getObjectById(leftKey);}
>
> finally{
>         pm.close();
>
> }
>
> This does not work. Documentation 
> athttp://code.google.com/appengine/docs/java/datastore/creatinggettinga...
> states you must provide a class identifier to retrieve objects using
> keys, so call
>
> SimpleObject retriveved = pm.getObjectById(SimpleObject.class,
> leftKey);
> And that works, indeed.
>
> The point is in the real application. I need to retrieve objects
> without knowing the class, then treat them differently according to
> their type. Actually I must model something similar to a tree (which
> is not possible to do without using key references due to the way keys
> are generated). I need to save the keys for left and right child of
> the tree, then retrieve the objects and treat inner nodes differently
> from leafs.
>
> Are there workarounds to get an object using its key in JDO? Maybe I
> should use low-level API to detect the kind of the entity.
>
> Thanks for any advice
> Lorenzo

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" group.
To post to this group, send email to google-appengine-j...@googlegroups.com.
To unsubscribe from this group, send email to 
google-appengine-java+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.

Reply via email to