Neo4j - 节点的Eager加载关系

时间:2013-03-01 00:48:33

标签: neo4j spring-data-neo4j

我有这样的NodeEntity:

@NodeEntity
public class User {

    @GraphId
    private Long id;

    private String name;

    @Fetch
    @RelatedToVia
    private Set<Rel> rels;

    public Rel connectTo(User u, String type) {
        if (this.rels == null) 
            this.rels = new HashSet<Rel>();
        Rel rel = new Rel(this, u, type);
        this.rels.add(rel);
        return rel;
    }
}

和RelationshipEntity:

@RelationshipEntity(type="REL")
public class Rel {

    @GraphId
    private Long id;
    @Fetch
    @StartNode
    private User start;
    @Fetch
    @EndNode
    private User end;

    @RelationshipType
    private String type;

    public Rel(){}
    public Rel(User start, User end, String type) {
        this.start = start;
        this.end = end;
        this.type = type;
    }
}

但是当我尝试加载用户时,关系是空的:

User neo = new User();
neo.setName("Neo");

User trinity = new User();
trinity.setName("Trinity");
this.userRepository.save(trinity);

neo.connectTo(trinity, "LOVES");

this.userRepository.save(neo);

User user = this.userRepository.findOne(neo.getId());
// expected:<1> but was:<0>
Assert.assertEquals(1, user.getRels().size());

我是否可以急切地加载与此节点相关的关系以及我是如何错过的?

提前致谢!

1 个答案:

答案 0 :(得分:0)

您可以尝试使用相同的关系类型吗?你的关系实体给出了type =“REL”,但你将“LOVES”传递给neo.connectTo()方法。