Retrieving parent from child

448 views
Skip to first unread message

Kaushik Bhandankar

unread,
Apr 27, 2011, 8:04:55 PM4/27/11
to objectify-appengine
Hello,

I have the following parent child relationship (with getter/setter for each field)

***********************************************************************
class ParentClass {
  @Id
  private Long id;
}

class ChildClass {
  @Id
  private Long id;
  
  @Parent
  private Key<ParentClass> parent;

  private int value;
}
***********************************************************************

Now, I persist the ParentClass and the ChildClass instances like below (using com.googlecode.objectify.helper.DAOBase)

***********************************************************************
// Persist the parent entity.
ParentClass pClass = new ParentClass();
ofy().put(pClass);

// Persist the child entity.
ChildClass cClass = new ChildClass();
Key<ParentClass> pKey = new Key<ParentClass>(ParentClass.class, pClass.getId());
cClass.setParent(pKey);
cClass.setValue(100);
ofy().put(cClass);
***********************************************************************

It works fine till here and I can see the data in datastore. Now, I try to retrieve the child and parent from datastore, like below.

***********************************************************************
// Retrieve child entity *based on its value*
Query<ChildClass> query = ofy.query(ChildClass.class).filter("value", 100);
ChildClass objChildClass = query.get();

// Retrieve parent entity from the child.
Key<ParentClass> objParentKey = objChildClass.getParent();
ParentClass objParentClass = ofy.get(ParentClass.class, objParentKey.getId());
***********************************************************************

Now, retrieving the parent entity from the child entity seems to fail for me (ie, objParentClass is null for me). Am I doing something wrong?

Kaushik Bhandankar

unread,
Apr 27, 2011, 8:12:51 PM4/27/11
to objectify-appengine
Pardon my typos. I mean ofy().* from com.googlecode.objectify.helper.DAOBase, instead of ofy.*

Jeff Schnitzer

unread,
Apr 27, 2011, 8:19:14 PM4/27/11
to objectify...@googlegroups.com
There is nothing obviously wrong to my eyes, other than that you can
just call ofy.get(objChildClass.getParent()) rather than the
two-parameter version. That shouldn't matter.

This seems far too obvious to be a genuine bug though. Have you
inspected the parent key id value? Is this *really* a complete test
case?

Jeff

Kaushik Bhandankar

unread,
Apr 27, 2011, 9:51:22 PM4/27/11
to objectify...@googlegroups.com
Thanks Jeff.

I lied initially,

I have a 4-layer hierarchy A -> B -> C -> D (where A -> B denotes that A is parent of B). In my previous example, D is ChildClass and C is ParentClass

Now, I persist this to database setting the parent properly for each class and it persists fine. I retrieve D based on value (works fine and D's ID matches the value in datastore) and want to retrieve C but this is failing.

I did some debugging using Eclipse debugger and can see that objParentKey.getId() has value 3 (matches datastore Id value for ParentClass or C) and this id gets passed to com.googlecode.objectify.impl.ObjectifyImpl.java's get(clazz, id) method and goes down to com.googlecode.objectify.ObjectifyImpl's get(clazz, id).

After stepping into the code while debugging, I can see that when com.google.objectify.Key.Key(parent, kind, id) gets invoked, kind (ParentClass) and id (=3) are correct per datastore but parent is null (should have been B). Is there something fundamental that I am missing?

Kaushik Bhandankar

unread,
Apr 27, 2011, 11:37:59 PM4/27/11
to objectify...@googlegroups.com
Solved this.

Looks like while doing ofy().get(), I was not passing the entire key for C (including its parents). Thanks for the pointers.
Reply all
Reply to author
Forward
0 new messages