在Spring Data Neo4j中实现延迟加载 [英] Implementing Lazy Loading in Spring Data Neo4j

查看:110
本文介绍了在Spring Data Neo4j中实现延迟加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring Data Neo4j 没有延迟加载.

我还是想在项目中延迟加载.毕竟,如果我不能依靠吸气剂每次都能真正得到我想要的吸气剂,那么到底有什么意义呢?

因此,为了使域模型被延迟加载,我正在考虑将它们注释为spring组件,并向我的getter添加逻辑以在尝试访问它们时对字段进行延迟加载.我知道这将使我的模型与neo4j牢固地耦合在一起,但是我宁愿具有这种强大的耦合性而不是没有延迟加载.

但是,在开始将所有模型转换为使用此模型之前,我想看看除了紧密耦合之外,是否有人可以告诉我这样做的任何问题. Spring数据在作为Spring组件的模型中应该没有任何问题,对吗?如果需要的话,我可以引用我从应用程序上下文中退出的服务的静态实例(我已经有了静态引用,因此不必使我的所有Vaadin前端类都成为spring组件.)最好使用您认为的那些?

解决方案

我认为所谓的高级映射模式可以完成您要完成的大部分工作.如果使用简单映射模式,则所有数据都将被复制到实体中,并且在使用@Fetch进行注释时,将提取所有关系.高级映射模式使您的实体成为节点周围的包装器,并且仅在您明确寻址实体时才访问基础节点.这样,许多数据将被延迟加载.确保在事务内处理您的实体,否则将为您进行的每个呼叫创建一个事务,从而影响您的绩效.

请参阅参考文档"大量有关此主题的重要信息.

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?

解决方案

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.

这篇关于在Spring Data Neo4j中实现延迟加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆