Neo4j/Spring-Data 中的懒惰/急切加载/获取 [英] Lazy/Eager loading/fetching in Neo4j/Spring-Data

查看:18
本文介绍了Neo4j/Spring-Data 中的懒惰/急切加载/获取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的设置,但遇到了一个令人费解的(至少对我而言)问题:

I have a simple setup and encountered a puzzling (at least for me) problem:

我有三个相互关联的 pojo:

I have three pojos which are related to each other:

@NodeEntity
public class Unit {
    @GraphId Long nodeId;
    @Indexed int type;
    String description;
}


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

    String name;

}

@NodeEntity
public class Worker {
    @GraphId Long nodeId;
    @Fetch User user;
    @Fetch Unit unit;
    String description;
}

所以你有一个带有currentunit"的 User-Worker-Unit,它在用户中标记,允许直接跳转到当前单位".每个User可以有多个worker,但是一个worker只能分配到一个unit(一个unit可以有多个worker).

So you have User-Worker-Unit with a "currentunit" which marks in user that allows to jump directly to the "current unit". Each User can have multiple workers, but one worker is only assigned to one unit (one unit can have multiple workers).

我想知道的是如何控制User.worker"上的 @Fetch 注释.我实际上希望只在需要时才使用它,因为大部分时间我只与工人"一起工作.

What I was wondering is how to control the @Fetch annotation on "User.worker". I actually want this to be laoded only when needed, because most of the time I only work with "Worker".

我浏览了 http://static.springsource.org/spring-data/data-neo4j/docs/2.0.0.RELEASE/reference/html/ 我不太清楚:

I went through http://static.springsource.org/spring-data/data-neo4j/docs/2.0.0.RELEASE/reference/html/ and it isn't really clear to me:

  • worker 是可迭代的,因为它应该是只读的(传入关系) - 在文档中清楚地说明了这一点,但在示例中大部分时间都使用了Set".为什么?或者没关系...
  • 如何让工作人员仅在访问时加载?(延迟加载)
  • 为什么我什至需要用@Fetch 注释简单的关系 (worker.unit).没有更好的办法吗?我有另一个具有许多这样简单关系的实体 - 我真的想避免仅仅因为我想要一个对象的属性而不得不加载整个图.
  • 我是否缺少 spring 配置以使其适用于延迟加载?
  • 有没有办法通过额外调用加载任何关系(未标记为@Fetch)?

从我看来,只要我想要一个 Worker,这个构造就会加载整个数据库,即使我大部分时间都不关心用户.

From how I see it, this construct loads the whole database as soon as I want a Worker, even if I don't care about the User most of the time.

我发现的唯一解决方法是使用存储库并在需要时手动加载实体.

The only workaround I found is to use repository and manually load the entities when needed.

------- 更新-------

我已经使用 neo4j 工作了很长时间,并找到了解决上述问题的方法,该解决方案不需要一直调用 fetch(因此不会加载整个图形).唯一的缺点:这是一个运行时方面:

I have been working with neo4j quite some time now and found a solution for the above problem that does not require calling fetch all the time (and thus does not load the whole graph). Only downside: it is a runtime aspect:

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mapping.model.MappingException;
import org.springframework.data.neo4j.annotation.NodeEntity;
import org.springframework.data.neo4j.support.Neo4jTemplate;

import my.modelUtils.BaseObject;

@Aspect
public class Neo4jFetchAspect {

    // thew neo4j template - make sure to fill it 
    @Autowired private Neo4jTemplate template;

    @Around("modelGetter()")
    public Object autoFetch(ProceedingJoinPoint pjp) throws Throwable {
        Object o = pjp.proceed();
        if(o != null) {
            if(o.getClass().isAnnotationPresent(NodeEntity.class)) {
                if(o instanceof BaseObject<?>) {
                    BaseObject<?> bo = (BaseObject<?>)o;
                    if(bo.getId() != null && !bo.isFetched()) {
                        return template.fetch(o);
                    }
                    return o;
                }
                try {
                    return template.fetch(o);
                } catch(MappingException me) {
                    me.printStackTrace();
                }
            }
        }
        return o;
    }

    @Pointcut("execution(public my.model.package.*.get*())")
    public void modelGetter() {}

}

您只需要调整应应用方面的类路径:my.model.package..get())")

You just have to adapt the classpath on which the aspect should be applied: my.model.package..get())")

我将方面应用于我的模型类上的所有 get 方法.这需要一些先决条件:

I apply the aspect to ALL get methods on my model classes. This requires a few prerequesites:

  • 您必须在模型类中使用 getter(该方面不适用于公共属性 - 您无论如何都不应该使用)
  • 所有模型类都在同一个包中(所以你需要稍微调整一下代码) - 我想你可以调整过滤器
  • aspectj 作为运行时组件是必需的(使用 tomcat 时有点棘手) - 但它可以工作:)
  • 所有模型类都必须实现 BaseObject 接口,该接口提供:

  • You MUST use getters in your model classes (the aspect does not work on public attributes - which you shouldn't use anyways)
  • all model classes are in the same package (so you need to adapt the code a little) - I guess you could adapt the filter
  • aspectj as a runtime component is required (a little tricky when you use tomcat) - but it works :)
  • ALL model classes must implement the BaseObject interface which provides:

公共接口 BaseObject {公共布尔 isFetched();}

public interface BaseObject { public boolean isFetched(); }

这可以防止重复获取.我只是检查一个强制性的子类或属性(即名称或除 nodeId 之外的其他内容)以查看它是否被实际获取.Neo4j 将创建一个对象,但只填充 nodeId 并保持其他所有内容不变(因此其他所有内容均为 NULL).

This prevents double-fetching. I just check for a subclass or attribute that is mandatory (i.e. the name or something else except nodeId) to see if it is actually fetched. Neo4j will create an object but only fill the nodeId and leave everything else untouched (so everything else is NULL).

@NodeEntity
public class User implements BaseObject{
    @GraphId
    private Long nodeId;

        String username = null;

    @Override
    public boolean isFetched() {
        return username != null;
    }
}

如果有人找到了一种无需那种奇怪的解决方法就可以做到这一点的方法,请添加您的解决方案:) 因为这个方法有效,但我会喜欢没有 aspectj 的方法.

If someone finds a way to do this without that weird workaround please add your solution :) because this one works, but I would love one without aspectj.

不需要自定义字段检查的基础对象设计

一种优化是创建一个基类而不是一个实际使用布尔字段(加载布尔值)并检查它的接口(因此您无需担心手动检查)

One optimization would be to create a base-class instead of an interface that actually uses a Boolean field (Boolean loaded) and checks on that (so you dont need to worry about manual checking)

public abstract class BaseObject {
    private Boolean loaded;
    public boolean isFetched() {
        return loaded != null;
    }
    /**
     * getLoaded will always return true (is read when saving the object)
     */
    public Boolean getLoaded() {
        return true;
    }

    /**
     * setLoaded is called when loading from neo4j
     */
    public void setLoaded(Boolean val) {
        this.loaded = val;
    }
}

这是有效的,因为在保存对象时返回true"以进行加载.当方面查看对象时,它使用 isFetched() 这 - 当对象尚未检索时将返回 null.检索到对象后,将调用 setLoaded 并将加载的变量设置为 true.

This works because when saving the object "true" is returned for loaded. When the aspect looks at the object it uses isFetched() which - when the object is not yet retrieved will return null. Once the object is retrieved setLoaded is called and the loaded variable set to true.

如何防止jackson触发延迟加载?

(作为对评论中问题的回答 - 请注意,我还没有尝试过,因为我没有遇到这个问题).

(As an answer to the question in the comment - note that I didnt try it out yet since I did not have this issue).

对于jackson,我建议使用自定义序列化程序(参见即http://www.baeldung.com/jackson-custom-serialization).这允许您在获取值之前检查实体.您只需检查它是否已被获取,然后继续整个序列化或仅使用 id:

With jackson I suggest to use a custom serializer (see i.e. http://www.baeldung.com/jackson-custom-serialization ). This allows you to check the entity before getting the values. You simply do a check if it is already fetched and either go on with the whole serialization or just use the id:

public class ItemSerializer extends JsonSerializer<BaseObject> {
    @Override
    public void serialize(BaseObject value, JsonGenerator jgen, SerializerProvider provider)
      throws IOException, JsonProcessingException {
        // serialize the whole object
        if(value.isFetched()) {
            super.serialize(value, jgen, provider);
            return;
        }
        // only serialize the id
        jgen.writeStartObject();
        jgen.writeNumberField("id", value.nodeId);
        jgen.writeEndObject();
    }
}

Spring 配置

这是我使用的示例 Spring 配置 - 您需要将包调整到您的项目:

This is a sample Spring configuration I use - you need to adjust the packages to your project:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/data/neo4j http://www.springframework.org/schema/data/neo4j/spring-neo4j-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <context:annotation-config/>
    <context:spring-configured/>

    <neo4j:repositories base-package="my.dao"/> <!-- repositories = dao -->

    <context:component-scan base-package="my.controller">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> <!--  that would be our services -->
    </context:component-scan>
    <tx:annotation-driven mode="aspectj" transaction-manager="neo4jTransactionManager"/>    
    <bean class="corinis.util.aspects.Neo4jFetchAspect" factory-method="aspectOf"/> 
</beans>

AOP 配置

这是用于此工作的/META-INF/aop.xml:

this is the /META-INF/aop.xml for this to work:

<!DOCTYPE aspectj PUBLIC
        "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
    <aspectj>
        <weaver>
            <!-- only weave classes in our application-specific packages -->
            <include within="my.model.*" />
        </weaver>
        <aspects>
            <!-- weave in just this aspect -->
            <aspect name="my.util.aspects.Neo4jFetchAspect" />
        </aspects>
    </aspectj>

推荐答案

我自己找到了所有问题的答案:

Found the answer to all the questions myself:

@Iterable:是的,iterable 可以用于只读

@Iterable: yes, iterable can be used for readonly

@load on access:默认情况下不加载任何内容.并且自动延迟加载不可用(至少据我所知)

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

其余的:当我需要建立关系时,我必须使用 @Fetch 或使用 neo4jtemplate.fetch 方法:

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
  }  
}

这篇关于Neo4j/Spring-Data 中的懒惰/急切加载/获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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