Neo4j/Spring数据中的懒惰/贪婪加载/获取



我有一个简单的设置,遇到了一个令人困惑的(至少对我来说)问题:

我有三个相互关联的pojo:

@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"上的@Fetch注释。实际上,我希望只有在需要时才使用它,因为大多数时候我只使用"worker"。

我经历了http://static.springsource.org/spring-data/data-neo4j/docs/2.0.0.RELEASE/reference/html/我并不清楚:

  • worker是可迭代的,因为它应该是只读的(传入关系)——在文档中,这是明确的,但在示例中,"Set"大部分时间都在使用。为什么?还是没关系
  • 如何使工作人员仅在访问时加载?(延迟加载)
  • 为什么我需要用@Fetch注释甚至是简单的关系(worker.unit)。难道没有更好的方法吗?我有另一个实体,它有很多这样简单的关系——我真的想避免因为我想要一个对象的属性而不得不加载整个图
  • 我是不是缺少了一个弹簧配置,所以它可以在延迟加载时工作
  • 有没有办法通过额外的调用加载任何关系(未标记为@Fetch)

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

我找到的唯一解决方法是使用存储库,并在需要时手动加载实体。

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

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

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.backage..get())

我将方面应用于我的模型类上的所有get方法。这需要一些预请求:

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

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

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

@NodeEntity
public class User implements BaseObject{
    @GraphId
    private Long nodeId;
        String username = null;
    @Override
    public boolean isFetched() {
        return username != null;
    }
}

如果有人找到了一种不用这种奇怪的变通方法的方法,请添加你的解决方案:)因为这个方法有效,但我喜欢没有aspectj的方法。

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

一种优化是创建一个基类,而不是一个实际使用布尔字段(加载了布尔字段)并检查该字段的接口(所以你不需要担心手动检查)

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。

如何防止jackson触发懒惰加载

(作为对评论中问题的回答——注意,我还没有尝试过,因为我没有这个问题)。

对于jackson,我建议使用自定义序列化程序(参见。http://www.baeldung.com/jackson-custom-serialization)。这允许您在获取值之前检查实体。您只需检查它是否已经被提取,然后继续整个序列化,或者只使用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配置示例-您需要根据您的项目调整包:

<?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,用于

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

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

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

@访问时加载:默认情况下不加载任何内容。并且自动延迟加载不可用(至少据我所知)

其余部分:当我需要一个关系时,我必须使用@Fetch或使用neo4jtemplate.Fetch方法:

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

不透明,但仍然延迟获取。

template.fetch(person.getDirectReports());

正如你在回答中所说的,@Fetch会进行热切的抓取。

我喜欢方面方法来绕过当前spring数据方式的限制来处理延迟加载。

@niko-我已经把你的代码样本放在一个基本的maven项目中,并试图让这个解决方案发挥作用,但收效甚微:

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

由于某些原因,Aspect正在初始化,但建议似乎没有得到执行。要重现问题,只需运行以下JUnit测试:

playground.neo4j.domain.UserTest

最新更新