What is the appropriate way to create objects with one-to-many relationships using Objectify and RequestFactory?

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!

+4
source share
1 answer

You need to do this as @Parent only when you are going to use it in a transaction against all Person in this Organization . I am sure that this is not what you want.

This is enough to save only the private Key<Organization> organizationKey and filter this field when you need to find Person for the specified Organization

As for downloading all the referenced objects, yes, this is necessary, you must download them manually. This is lavash, but it is not very much code.

In addition, there is another way of storing these relationships if your organization is small enough and consists of several hundred people. In this case, you can have List<Key<Person>> contactPeopleKey; and loading all these people with an existing Key , manually, is much faster than loading with a new request

+1
source

All Articles