Lazy/Eager loading/fetching in Neo4j/Spring-Data

你离开我真会死。 提交于 2019-11-28 07:36:01

Found the answer to all the questions myself:

@Iterable: yes, iterable can be used for readonly

@load on access: per default nothing is loaded. and automatic lazy loading is not available (at least as far as I can gather)

For the rest: When I need a relationship I either have to use @Fetch or use the neo4jtemplate.fetch method:

@NodeEntity
public class User {
    @GraphId Long nodeId;
    @RelatedTo(type="user", direction = Direction.INCOMING)
    private Iterable<Worker> worker;
    @Fetch Unit currentUnit;

    String name;

}

class GetService {
  @Autowired private Neo4jTemplate template;

  public void doSomethingFunction() {
    User u = ....;
    // worker is not avaiable here

    template.fetch(u.worker);
    // do something with the worker
  }  
}

Not transparent, but still lazy fetching.

template.fetch(person.getDirectReports());

And @Fetch does the eager fetching as was already stated in your answer.

I like the aspect approach to work around the limitation of the current spring-data way to handle lazy loading.

@niko - I have put your code sample in a basic maven project and tried to get that solution to work with little success:

https://github.com/samuel-kerrien/neo4j-aspect-auto-fetching

For some reasons the Aspect is initialising but the advice doesn't seem to get executed. To reproduce the issue, just run the following JUnit test:

playground.neo4j.domain.UserTest
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!