What is the appropriate way to create objects with one-to-many relationships using Objectify and RequestFactory? I read the documentation for these libraries, and also looked at a number of sample projects, such as listwidget and gwtgae2011 . They all use the @Embedded
annotation, which is not what I want, because it stores everything within the same object. Another option according to the documentation is to use the @Parent
property in child classes. In my example (the cleaners / setters are removed for simplicity) I have Person
and Organization
entities that are defined as
@Entity public class Person extends DatastoreObject { private String name; private String phoneNumber; private String email; @Parent private Key<Organization> organizationKey; }
and
@Entity public class Organization extends DatastoreObject { private String name; private List<Person> contactPeople; private String address; }
Now, if I understand the documentation correctly to migrate an organization with one Person, I need to migrate the organization first and then set organizationKey
to ObjectifyService.factory().getKey(organization)
for the Person object and then save it. I no longer like the fact that I have to iterate over each child object manually, but using RequestFactory makes it all the more confusing due to the presence of proxy classes. How can I define Organization and OrganizationProxy classes - with or without Key <>? Should I define something like this in an organization?
public void setContactPeople(List<Person> contactPeople) { for (int i = 0; i < contactPeople.size(); ++i) { DAOBase dao = new DAOBase(); Key<Organization> key = dao.ofy().put(this); contactPeople.get(i).setOrganizationKey(key); } this.contactPeople = contactPeople; }
And how can I load an Organization with its children from Datastore? Should I manually extract each person and populate the Organization.contactPeople method in @PostLoad
?
It looks like I will have to write a LOT of service code to do what JPA / JDO does behind the scenes. I just do not understand: (
Am I missing something or is this the only way to implement it?
Thanks for the answers in advance!
source share