title: RequestFactory theory: Why is Locator<>.find() being called so often?
body: <p>I'm new to RequestFactory but with generous help of <a href="https://stackoverflow.com/users/116472/thomas-broyer">Thomas Broyer</a> and after reviewing documents below it's getting much better :)</p>
  
  <ul>
  <li><a href="http://code.google.com/webtoolkit/doc/latest/DevGuideRequestFactory.html" rel="nofollow noreferrer">Getting Started with RequestFactory</a></li>
  <li><a href="http://code.google.com/p/google-web-toolkit/wiki/RequestFactoryMovingParts" rel="nofollow noreferrer">Request Factory Moving Parts</a></li>
  <li><a href="http://code.google.com/p/google-web-toolkit/wiki/RequestFactory_2_4" rel="nofollow noreferrer">RequestFactory changes in GWT 2.4</a></li>
  </ul>
  
  <p>But could you please explain why <code>Locator&lt;&gt;.find()</code> is being called so unnecessarily (in my opinion) often ?</p>
  
  <p>In my sample project I have two entities Organization and Person that maintain parent-child relationship. When I fetch Organization Objectify <strong>automatically</strong> fetches child Person. 
  Also I created two methods in my service layer <code>findOrganizationById</code> and <code>saveOrganization</code> that load and persist objects.</p>
  
  <p>Now consider two scenarios:</p>
  
  <p>When I call <code>findOrganizationById</code> in the client following calls occur on server side:</p>
  
  <pre><code>OrderDao.findOrganizationById(1)
  PojoLocator.getId(Key&lt;?&gt;(Organization(1)))
  PojoLocator.getId(Key&lt;?&gt;(Organization(1)/Person(2)))
  PojoLocator.getId(Key&lt;?&gt;(Organization(1)))
  PojoLocator.find(Key&lt;?&gt;(Organization(1)))
  PojoLocator.getId(Key&lt;?&gt;(Organization(1)/Person(2)))
  PojoLocator.find(Key&lt;?&gt;(Organization(1)/Person(2)))
  </code></pre>
  
  <p>By calling <code>OrderDao.findOrganizationById</code> I've already received full graph of objects. Why call <code>.find</code> twice in addition to that? It's extra load on Datastore that cost me money. Of course I cache it but it would be neat to fix it. How can I avoid these additional calls ?</p>
  
  <p>Similar thing happens when I save object(s) by calling <code>saveOrganization</code> in the client. Following calls occur on server side:</p>
  
  <pre><code>PojoLocator.find(Key&lt;?&gt;(Organization(1)))
  PojoLocator.find(Key&lt;?&gt;(Organization(1)/Person(2)))
  OrderDao.saveOrganization(1)
  PojoLocator.getId(Key&lt;?&gt;(Organization(1)))
  PojoLocator.find(Key&lt;?&gt;(Organization(1)))
  PojoLocator.getId(Key&lt;?&gt;(Organization(1)/Person(2)))
  PojoLocator.find(Key&lt;?&gt;(Organization(1)/Person(2)))
  </code></pre>
  
  <p>I can understand need for fetching two objects from DataStore <strong>before</strong> updating it. RequestFactory sends deltas to the server so it needs to have entire object before persisting it. Still since I load full graph at once it would be nice not to have second call which is <code>PojoLocator.find(Key&lt;?&gt;(Organization(1)/Person(2)))</code>. And I truly can't understand need for <code>.find()</code> calls <strong>after</strong> persisting.</p>
  
  <p>Thoughts ?</p>
  
  <p>My proxies</p>
  
  <pre><code>@ProxyFor(value = Organization.class, locator = PojoLocator.class)
  public interface OrganizationProxy extends EntityProxy
  {
      public String getName();
      public void setName(String name);
      public String getAddress();
      public void setAddress(String address);
      public PersonProxy getContactPerson();
      public void setContactPerson(PersonProxy contactPerson);
      public EntityProxyId&lt;OrganizationProxy&gt; stableId();
  }
  
  @ProxyFor(value = Person.class, locator = PojoLocator.class)
  public interface PersonProxy extends EntityProxy
  {
      public String getName();
      public void setName(String name);
      public String getPhoneNumber();
      public void setPhoneNumber(String phoneNumber);
      public String getEmail();
      public void setEmail(String email);
      public OrganizationProxy getOrganization();
      public void setOrganization(OrganizationProxy organization);
  }
  </code></pre>
  
  <p>My service</p>
  
  <pre><code>public interface AdminRequestFactory extends RequestFactory
  {
      @Service(value = OrderDao.class, locator = InjectingServiceLocator.class)
      public interface OrderRequestContext extends RequestContext
      {
          Request&lt;Void&gt; saveOrganization(OrganizationProxy organization);
          Request&lt;OrganizationProxy&gt; findOrganizationById(long id);
      }
  
      OrderRequestContext contextOrder();
  }
  </code></pre>
  
  <p>and finally my Locator&lt;></p>
  
  <pre><code>public class PojoLocator extends Locator&lt;DatastoreObject, String&gt;
  {
      @Inject Ofy ofy;
  
      @Override
      public DatastoreObject create(Class&lt;? extends DatastoreObject&gt; clazz)
      {
          try
          {
              return clazz.newInstance();
          } catch (InstantiationException e)
          {
              throw new RuntimeException(e);
          } catch (IllegalAccessException e)
          {
              throw new RuntimeException(e);
          }
      }
  
      @Override
      public DatastoreObject find(Class&lt;? extends DatastoreObject&gt; clazz, String id)
      {
          Key&lt;DatastoreObject&gt; key = Key.create(id);
          DatastoreObject load = ofy.load(key);
          return load;
      }
  
      @Override
      public Class&lt;DatastoreObject&gt; getDomainType()
      {
          return null;    // Never called
      }
  
      @Override
      public String getId(DatastoreObject domainObject)
      {
          Key&lt;DatastoreObject&gt; key = ofy.fact().getKey(domainObject);
          return key.getString();
      }
  
      @Override
      public Class&lt;String&gt; getIdType()
      {
          return String.class;
      }
  
      @Override
      public Object getVersion(DatastoreObject domainObject)
      {
          return domainObject.getVersion();
      }
  }
  </code></pre>