简体   繁体   中英

Spring Data Neo4J @Fetch Issue seen with Jackson Serialization

I'm trying to figure out why the Jackson JSON Serialization of a collection of 250 objects is taking 40 seconds, and I think I have narrowed it down to SDN lazy loading. I'm using @Fetch, but it still seems as if its asking the database for the delegate for every attribute of every node in the collection. Please ignore any typos as I have to hand-type this as copy-paste isn't an option. Rest assured the class compiles as expected. The (simplified) class being serialized:

@NodeEntity
public class NodeWithDelegate {

  @RelatedTo(type="REL_NAME", direction=Direction.OUTGOING)
  @Fetch private DelegateNode delegate;

  private DelegateNode getInitializedDelegate() {
    if (delegate == null) {
      delegate = new DelegateNode();
    }
    return delegate;
  }

  public String getDelegateAttribute1() {
    return delegate == null ? null : delegate.getAttribute1();
  }

  public void setDelegateAttribute1(String attribute1) {
    getInitializedDelegate().setAttribute1(attribute1);
  }

  .... 

  public String getDelegateAttribute15() {
    return delegate == null ? null : delegate.getAttribute15();
  }

  public void setDelegateAttribute15(String attribute15) {
    getInitializedDelegate().setAttribute15(attribute15);
  }
}

The DelegateNode class is exactly what you would expect, just a simple @NodeEntity POJO containing 15 String or Integer or Boolean attributes.

So two questions really:

  1. how can I tell for sure if an object is actually being eagerly loaded? I'm using eclipse.
  2. For debugging purposes, if the objects are all eagerly loaded, and I put a breakpoint between the fetching of the collection from the database and the serializer which calls all the delegate getters, and while paused shutdown the database, should it work? Is there any reason the objects would need to talk to the database at this point if its all eagerly loaded?

I guess I should mention I'm using the rest api for neo4j.

Many thanks in advance!

I am assuming you are using 3.x version of Spring Data Neo4j.

This version is not very optimized for REST api. If you enable logging of the cypher queries you will see many. Example for log4j:

log4j.category.org.springframework.data.neo4j.support.query=DEBUG

You can work around this limitation using custom cypher query and mapping the result with @QueryResult annotation.

  1. Using the logging you should see your objects being loaded

  2. It should, unless there is something "lazy" in the DelegateNode itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM