Implementing Lazy Loading in Spring Data Neo4j

拟墨画扇 提交于 2019-12-24 14:41:52

问题


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?


回答1:


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.




回答2:


Not transparent, but still lazy fetching.

template.fetch(person.getDirectReports());



回答3:


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



来源:https://stackoverflow.com/questions/15564880/implementing-lazy-loading-in-spring-data-neo4j

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