GAE Datastore: persisting referenced objects

Posted by David on Stack Overflow See other posts from Stack Overflow or by David
Published on 2010-06-04T13:38:04Z Indexed on 2010/06/06 5:52 UTC
Read the original article Hit count: 395

Two "I'm sorries" to begin with:
1) I've looked for a solution (here, and elsewhere), and couldn't find the answer.
2) English is not my mother tongue, so I may have some typos and the sort - please ignore them.

To the point:
I am trying to persist Java objects to the GAE datastore.
I am not sure as to how to persist object having ("non-trivial") referenced object.
That is, assume I have the following.

public class Father {
String name;
int age;
Vector<Child> offsprings; //this is what I call "non-trivial" reference 
//ctor, getters, setters...

}

public class Child {
String name;
int age;
Father father; //this is what I call "non-trivial" reference    
//ctor, getters, setters...

}
The name field is unique in each type domain, and is considered a Primary-Key.
In order to persist the "trivial" (String, int) fields, all I need is to add the correct annotation. So far so good.
However, I don't understand how should I persist the home-brewed (Child, Father) types referenced.
Should I:

  1. Convert each such reference to hold the Primary-Key (a name String, in this example) instead of the "actual" object.
    So, Vector<Child> offsprings; becomes Vector<String> offspringsNames; ?
    If that is the case, how do I handle the object at run-time? Do I just query for the Primary-Key from Class.getName, to retrieve the refrenced objects?
  2. Convert each such reference to hold the actual Key provided to me by the Datastore upon the proper put() operation?
    So, Vector<Child> offsprings; becomes Vector<Key> offspringsHashKeys; ?

I would very much appreciate all kinds of comments.
I have read ALL the offical relevant GAE docs/example. Throughout, they always persist "trivial" references, natively supported by the Datastore (e.g. in the Guestbook example, only Strings, and Longs).
Many thanks,
David

© Stack Overflow or respective owner

Related posts about java

Related posts about google-app-engine