title: Lazy/Eager loading/fetching in Neo4j/Spring-Data
body: <p>I have a simple setup and encountered a puzzling (at least for me) problem:</p>
  
  <p>I have three pojos which are related to each other:</p>
  
  <pre><code>@NodeEntity
  public class Unit {
      @GraphId Long nodeId;
      @Indexed int type;
      String description;
  }
  
  
  @NodeEntity
  public class User {
      @GraphId Long nodeId;
      @RelatedTo(type="user", direction = Direction.INCOMING)
      @Fetch private Iterable&lt;Worker&gt; worker;
      @Fetch Unit currentUnit;
  
      String name;
  
  }
  
  @NodeEntity
  public class Worker {
      @GraphId Long nodeId;
      @Fetch User user;
      @Fetch Unit unit;
      String description;
  }
  </code></pre>
  
  <p>So you have User-Worker-Unit with a "currentunit" which marks in user that allows to jump directly to the "current unit". Each User can have multiple workers, but one worker is only assigned to one unit (one unit can have multiple workers).</p>
  
  <p>What I was wondering is how to control the <strong>@Fetch</strong> annotation on "User.worker". I actually want this to be laoded only when needed, because most of the time I only work with "Worker".</p>
  
  <p>I went through <a href="http://static.springsource.org/spring-data/data-neo4j/docs/2.0.0.RELEASE/reference/html/" rel="nofollow noreferrer">http://static.springsource.org/spring-data/data-neo4j/docs/2.0.0.RELEASE/reference/html/</a> and it isn't really clear to me:</p>
  
  <ul>
  <li>worker is iterable because it should be read only (incoming relation) - in the documentation this is stated clarly, but in the examples ''Set'' is used most of the time. Why? or doesn't it matter...</li>
  <li>How do I get worker to only load on access? (lazy loading)</li>
  <li>Why do I need to annotate even the simple relations (worker.unit) with @Fetch. Isn't there a better way? I have another entity with MANY such simple relations - I really want to avoid having to load the entire graph just because i want to the properties of one object.</li>
  <li>Am I missing a spring configuration so it works with lazy loading?</li>
  <li>Is there any way to load any relationships (which are not marked as @Fetch) via an extra call?</li>
  </ul>
  
  <p>From how I see it, this construct loads the whole database as soon as I want a Worker, even if I don't care about the User most of the time.</p>
  
  <p>The only workaround I found is to use repository and manually load the entities when needed.</p>
  
  <p><strong>------- Update -------</strong></p>
  
  <p>I have been working with neo4j quite some time now and found a solution for the above problem that does not require calling fetch all the time (and thus does not load the whole graph). Only downside: it is a runtime aspect:</p>
  
  <pre><code>import org.aspectj.lang.ProceedingJoinPoint;
  import org.aspectj.lang.annotation.Around;
  import org.aspectj.lang.annotation.Aspect;
  import org.aspectj.lang.annotation.Pointcut;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.data.mapping.model.MappingException;
  import org.springframework.data.neo4j.annotation.NodeEntity;
  import org.springframework.data.neo4j.support.Neo4jTemplate;
  
  import my.modelUtils.BaseObject;
  
  @Aspect
  public class Neo4jFetchAspect {
  
      // thew neo4j template - make sure to fill it 
      @Autowired private Neo4jTemplate template;
  
      @Around("modelGetter()")
      public Object autoFetch(ProceedingJoinPoint pjp) throws Throwable {
          Object o = pjp.proceed();
          if(o != null) {
              if(o.getClass().isAnnotationPresent(NodeEntity.class)) {
                  if(o instanceof BaseObject&lt;?&gt;) {
                      BaseObject&lt;?&gt; bo = (BaseObject&lt;?&gt;)o;
                      if(bo.getId() != null &amp;&amp; !bo.isFetched()) {
                          return template.fetch(o);
                      }
                      return o;
                  }
                  try {
                      return template.fetch(o);
                  } catch(MappingException me) {
                      me.printStackTrace();
                  }
              }
          }
          return o;
      }
  
      @Pointcut("execution(public my.model.package.*.get*())")
      public void modelGetter() {}
  
  }
  </code></pre>
  
  <p>You just have to adapt the classpath on which the aspect should be applied: my.model.package.<em>.get</em>())")</p>
  
  <p>I apply the aspect to ALL get methods on my model classes. This requires a few prerequesites:</p>
  
  <ul>
  <li>You MUST use getters in your model classes (the aspect does not work on public attributes - which you shouldn't use anyways)</li>
  <li>all model classes are in the same package (so you need to adapt the code a little) - I guess you could adapt the filter</li>
  <li>aspectj as a runtime component is required (a little tricky when you use tomcat) - but it works :)</li>
  <li><p>ALL model classes must implement the BaseObject interface which provides:</p>
  
  <p>public interface BaseObject {
      public boolean isFetched();
  }</p></li>
  </ul>
  
  <p>This prevents double-fetching. I just check for a subclass or attribute that is mandatory (i.e. the name or something else except nodeId) to see if it is actually fetched. Neo4j will create an object but only fill the nodeId and leave everything else untouched (so everything else is NULL).</p>
  
  <p>i.e.</p>
  
  <pre><code>@NodeEntity
  public class User implements BaseObject{
      @GraphId
      private Long nodeId;
  
          String username = null;
  
      @Override
      public boolean isFetched() {
          return username != null;
      }
  }
  </code></pre>
  
  <p>If someone finds a way to do this without that weird workaround please add your solution :) because this one works, but I would love one without aspectj.</p>
  
  <p><strong>Base object design that doenst require a custom field check</strong></p>
  
  <p>One optimization would be to create a base-class instead of an interface that actually uses a Boolean field (Boolean loaded) and checks on that (so you dont need to worry about manual checking)</p>
  
  <pre><code>public abstract class BaseObject {
      private Boolean loaded;
      public boolean isFetched() {
          return loaded != null;
      }
      /**
       * getLoaded will always return true (is read when saving the object)
       */
      public Boolean getLoaded() {
          return true;
      }
  
      /**
       * setLoaded is called when loading from neo4j
       */
      public void setLoaded(Boolean val) {
          this.loaded = val;
      }
  }
  </code></pre>
  
  <p>This works because when saving the object "true" is returned for loaded. When the aspect looks at the object it uses isFetched() which - when the object is not yet retrieved will return null. Once the object is retrieved setLoaded is called and the loaded variable set to true. </p>
  
  <p><strong>How to prevent jackson from triggering the lazy loading?</strong></p>
  
  <p>(As an answer to the question in the comment - note that I didnt try it out yet since I did not have this issue).</p>
  
  <p>With jackson I suggest to use a custom serializer (see i.e. <a href="http://www.baeldung.com/jackson-custom-serialization" rel="nofollow noreferrer">http://www.baeldung.com/jackson-custom-serialization</a> ). This allows you to check the entity before getting the values. You simply do a check if it is already fetched and either go on with the whole serialization or just use the id:</p>
  
  <pre><code>public class ItemSerializer extends JsonSerializer&lt;BaseObject&gt; {
      @Override
      public void serialize(BaseObject value, JsonGenerator jgen, SerializerProvider provider)
        throws IOException, JsonProcessingException {
          // serialize the whole object
          if(value.isFetched()) {
              super.serialize(value, jgen, provider);
              return;
          }
          // only serialize the id
          jgen.writeStartObject();
          jgen.writeNumberField("id", value.nodeId);
          jgen.writeEndObject();
      }
  }
  </code></pre>
  
  <p><strong>Spring Configuration</strong></p>
  
  <p>This is a sample Spring configuration I use - you need to adjust the packages to your project:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:context="http://www.springframework.org/schema/context"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"
         xmlns:tx="http://www.springframework.org/schema/tx"
         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
  http://www.springframework.org/schema/data/neo4j http://www.springframework.org/schema/data/neo4j/spring-neo4j-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"&gt;
  
      &lt;context:annotation-config/&gt;
      &lt;context:spring-configured/&gt;
  
      &lt;neo4j:repositories base-package="my.dao"/&gt; &lt;!-- repositories = dao --&gt;
  
      &lt;context:component-scan base-package="my.controller"&gt;
          &lt;context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/&gt; &lt;!--  that would be our services --&gt;
      &lt;/context:component-scan&gt;
      &lt;tx:annotation-driven mode="aspectj" transaction-manager="neo4jTransactionManager"/&gt;    
      &lt;bean class="corinis.util.aspects.Neo4jFetchAspect" factory-method="aspectOf"/&gt; 
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>AOP config</strong></p>
  
  <p>this is the /META-INF/aop.xml for this to work:</p>
  
  <pre><code>&lt;!DOCTYPE aspectj PUBLIC
          "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd"&gt;
      &lt;aspectj&gt;
          &lt;weaver&gt;
              &lt;!-- only weave classes in our application-specific packages --&gt;
              &lt;include within="my.model.*" /&gt;
          &lt;/weaver&gt;
          &lt;aspects&gt;
              &lt;!-- weave in just this aspect --&gt;
              &lt;aspect name="my.util.aspects.Neo4jFetchAspect" /&gt;
          &lt;/aspects&gt;
      &lt;/aspectj&gt;
  </code></pre>