1

I am using Spring-data to access a Neo4j database via REST.

One of my entities looks similar to the following one:

@NodeEntity
@TypeAlias("org.example.Foo")
public class Foo {
   @GraphId
   private Long nodeId;

   //...

   @RelatedTo(type="HAS_BAR", direction=Direction.OUTGOING)
   private Set<Bar> bars; 

   //...
}

A typical Foo might have anywhere from 0-1000 Bars. Most of the time, those Bars are not needed when loading a Foo so I thought I should by fine by not adding a @Fetch annotation and thus avoiding to eager-load the Bars.

However, when now loading a Foo using the generated repository methods, the Bars are loaded - at least partially (only their nodeId properties).

Is there any way to avoid this? Performance suffers quite much from this behavior.
I really would like to be able to use lazy-loading like shown in https://stackoverflow.com/a/16159051/232175 for the collection itself.

Community
  • 1
  • 1
Matthias
  • 12,053
  • 4
  • 49
  • 91

3 Answers3

1

For the lazy-fetching to work, spring data creates a proxy for all the Bar's with just enough information (node id) that can be used to lazily fetch the Bar's when required. That is why the Bar's are being created in your case. I suggest you use the Neo4jTemplate to pull just the Foo's properties that you are looking for as shown below

Result<Map<String, Object>> result = template.query("START n=node({0}) RETURN n.property1, n.property2, n.property3");
result.handle(new Handler<Map<String, Object>>()
{
    @Override
    public void handle(Map<String, Object> row)
    {
        System.err.println(row.get("n.property1"));
        System.err.println(row.get("n.property2"));
        System.err.println(row.get("n.property3"));
    }
});
Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327
  • Thanks for your response! Hmm - then there wouldn't be too much difference from stopping using spring-data and doing the mapping the "old", manual way, would it? – Matthias Aug 27 '13 at 01:13
0

If you don't any particular reason to use neo4j via rest you can use it embedded with the aspectj mapping that doesn't have this problem. You could use it also via REST but according to this post Neo4j Spring data POC for social RESTful layer is better to avoid it.

Community
  • 1
  • 1
alex
  • 3,412
  • 2
  • 28
  • 36
  • Thanks, but the database needs to be shared by multiple applications, so REST is a must in this case. – Matthias Aug 29 '13 at 01:45
0

If you dont need the Bars most of the time, remove them from your entity and and just load them if needed with cypher?

Apart from that, spring-data-neo4j doesnt support explicit lazy loading in simple mode, but you might try your luck with the advanced mapping mode(http://static.springsource.org/spring-data/data-graph/snapshot-site/reference/html/#reference:aspectj)

PhilBa
  • 732
  • 4
  • 16