Java – gae data store: persistent referenced objects
I tried to persist Java objects to the gae data store
I'm not sure how to persist objects with ("nontrivial") reference objects In other words, suppose 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,setters...
}
The name field is unique in each type field and is treated as a primary key
In order to maintain the "normal" (string, int) field, I just need to add the correct comments So far, it's very good However, I don't understand how I should stick to the reference homemade (children, father) type Should I:
>Convert each such reference to hold the primary key (in this example, the name string) instead of the "actual" object, so vector < child > descendants; Change to vector < string > offspringsnames;? If this is the case, how do I handle objects at run time? I'm just from class Getname queries the primary key to retrieve the referenced object? > Convert each such reference to save the actual key provided to me when the data is stored in the correct put () operation? That is, vector < child > offspring; Change to vector < key > offspringshashkeys;?
I have read all official relevant gae documents / examples Throughout the process, they maintain "trivial" references that are locally supported by the data store (for example, in the guestbook example, strings and long integers only)
Solution
>Please refer to Google App Engine docs in the following sections for a clearer understanding (relationship, transaction)
For your question, you have several choices:
>Have a one to many relationship (objects will be in the same entity group). Here, you can have a child list in the parent (parent) This places all objects in the same entity group If you do not want to get children every time you get a parent, you can remove children from the default get group
@PersistenceCapable(identityType = IdentityType.APPLICATION,detachable = "true")
public class Father {
@PrimaryKey
@Persistent
private String name;
@Persistent
private int age;
@Persistent(mappedBy = "father",defaultFetchGroup = "false")
private List childern;
}
@PersistenceCapable(identityType = IdentityType.APPLICATION,detachable = "true")
public class Child {
@Persistent
@PrimaryKey
private String name;
@Persistent
private Father dad;
}
>Unrelated relationships that store keys instead of references:
@PersistenceCapable(identityType = IdentityType.APPLICATION,detachable = "true")
public class Father {
@PrimaryKey
@Persistent
private String name;
@Persistent
private int age;
@Persistent
private List childern;
}
@PersistenceCapable(identityType = IdentityType.APPLICATION,detachable = "true")
public class Child {
@Persistent
@PrimaryKey
private String name;
@Persistent
private Key dad;
}
In this case, you must manage referential integrity and ensure that they are in the same entity group if you have to update / add them in a single transaction
IMO, if I am modeling a real-world (father child) scenario, I will choose the route of "having a relationship", because, really, how many children can a person have;) Of course, there is another question, that is, how many fathers do you update at a time?
Hope this will help, cheer!
