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've read the documentation for these libraries and looked at some sample projects, such as list widget and gwtgae 2011 All of this uses the @ embedded annotation, which is not what I want because it stores everything in one entity Another option based on the document is to use the @ parent attribute in subclasses In my example (getters / setters removed for simplicity), I define the entities person and organization 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 document correctly in order to use a person persistent organization, I must first maintain the organization and then set the organizationkey to objectifyservice factory(). GetKey (Organization) is used for the person object and then keeps it I don't like that I have to iterate each sub object manually, but using requestfactory will make everything more complicated because of the existence of proxy classes How to define the organization and organizationproxy classes – use key < > or not? Do I have to define such content in the 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;
}

How do I load an organization and its children from the datastore? Do I have to get each person manually and fill in organization. In the @ postload method contactPeople?

It seems that I have to write a lot of maintenance code to perform what JPA / JDO does behind the scenes I can't get it at all:(

What have I missed, or is this the only way to achieve it?

Thank you very much for answering in advance!

Solution

It needs to be set to @ parent. Only if you want to use it in transactions for all persons in the organization I'm sure it's not what you want

Just save the private key < organization > Organizationkey: filter by this field when you need to find a person for the specified organization

As for loading all referenced objects - yes, it is, you have to load it manually It's a pita pie, but it's not a lot of code

In addition, if your organization is small enough and consists of hundreds of people, there are different ways of this relationship In this case, you can have list < key < person > > contactpeoplekey;, And manually load all these people through the existing key, which is much faster than the new query

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>