3

The GAE-JDO docs suggest it is possible to retrieve parent keys from child keys:

Note also that a key's string representation is not encrypted: a user can decode the key string to extract its components, including the kinds and identifiers of the entity and its ancestors.

REF: "Entities, Properties, and Keys"

I am generating keys using the following:

@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
@Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
private String id;

When inspecting the datastore, my entity keys look like the following:

parent.id = agpzfnZpdmVlbGFichQLEgdTZXNzaW9uGICAgICgwMsIDA
child1.id = agpzfnZpdmVlbGFiciYLEgdTZXNzaW9uGICAgICgwMsIDAsSBVN0YWdlGICAgICAwK8KDA
...

(where Parent has children of type Child). So, what I'm looking for is a GWT function like this:

String getParentIdFromChildId(String childId) {
    String parentId = ...        
    return parentId;
}

so that from the client (GWT) I can reference the child object (by first finding its parent):

Child child = data.getParent(getParentIdFromChildId(childId)).getChild(childId);

I could solve this problem by also keeping track of the parent id, but if the parent information is already embedded in the child id, this is inefficient.

Thanks in advance.

~Owen

3
  • Have you found the algorithm or a routine to do that in Java or python?, if so the java version should work directly in gwt or should be easy to port. Feb 19, 2013 at 12:14
  • No, haven't found how to do it in Java or Python.
    – opowell
    Feb 19, 2013 at 15:30
  • it seems the child id is composed by a string appended to the parent one, if the pattern were that the child id starts in a fixed position the algo was trivial, but I'm not convinced. Try to look for more docs around that or playing with more keys. Feb 19, 2013 at 20:14

1 Answer 1

1

child1.getParent() returns the key of the parent of the entity child1.

So if you had an entity child1, and you want its parent's id, just call:

String parentId = child1.getParent().id;

to return it's parent's id.

Read more here: Entity 'getParent()' Method Documentation

Not sure if that's what you wanted, but that is how I would do it.

Thanks,

~Samir

1
  • Your solution works for App Engine - I require a GWT solution.
    – opowell
    Jun 12, 2013 at 9:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.