1

Spring Data Neo4j doesn't have lazy loading.

I want lazy loading in my project anyway. After all, what's really the point of having a getter if I can't rely on it to actually get what I want it to get every time?

So to make my domain models be lazy loaded, I was thinking of annotating them as spring components and adding logic to my getters that lazy loads fields when I try to access them. I know this will strongly couple my models to neo4j, but I'd rather have that strong coupling than no lazy loading.

Before I start converting all my models over to using this, though, I wanted to see if anyone could tell me any problems with doing this aside from the tight coupling. Spring data shouldn't have any issues with my models also working as spring components, right? If I wanted to, I could reference a static instance of my services I get out of the application context (I already have the static references so I didn't have to make all my Vaadin front-end classes spring components.) Would it be better to use those do you think?

Community
  • 1
  • 1
CorayThan
  • 17,174
  • 28
  • 113
  • 161

3 Answers3

2

I think the so called advanced mapping mode does most of what you want to accomplish out of the box. If you use the simple mapping mode, all data will be copied into entities and all relationships will be fetched when they're annotated with @Fetch. Advanced mapping mode makes your entities a kind of wrapper around your node and only accesses the underlying node when you explicitly address your entity. This way, a lot of data will be lazily loaded. Make sure you address your entity inside a transaction, otherwise a transaction will be created for every call you make, thus impacting your performance.

See the reference documentation for loads of great information about this subject.

tstorms
  • 4,941
  • 1
  • 25
  • 47
1

Not transparent, but still lazy fetching.

template.fetch(person.getDirectReports());
s_t_e_v_e
  • 2,496
  • 3
  • 31
  • 35
0

Some updates based on the current neo4j reference - http://docs.spring.io/spring-data/neo4j/docs/current/reference/html/

@Fetch is a Obsolete annotation

neo4jTemplate.fetch() is not available, you should specify the depth - http://docs.spring.io/spring-data/neo4j/docs/current/reference/html/#_api_changes

Gui
  • 72
  • 9
  • @Fetch - http://docs.spring.io/spring-data/neo4j/docs/current/reference/html/#_obsolete_annotations – Gui Jun 14 '16 at 10:02