3
votes

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

1
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. - Manolo Carrasco Moñino
No, haven't found how to do it in Java or Python. - opowell
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. - Manolo Carrasco Moñino

1 Answers

1
votes

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