如何在SDN4中获取自定义查询中的直接关系实体和直接相关节点?

时间:2015-11-04 15:32:19

标签: neo4j spring-data-neo4j-4

我的存储库中有一个带注释的finder方法:

@Query("MATCH (me:User)<-[ab:ASKED_BY]-(q:Question) WHERE id(me) = {0} RETURN q")
Iterable<Question> findQuestionsByUserId(Long id);

我的对象如:

@NodeEntity
public class Question {
    private AskedBy askedBy;

    @Relationship(type = "TAGGED_WITH")
    private Set<Tag> tags = new HashSet<>();
//...
}

@RelationshipEntity(type = "ASKED_BY")
public class AskedBy {
    @GraphId private Long id;

    @StartNode
    private User user;

    @EndNode
    private Question question;

    // other props
}

当我调用存储库方法时,结果中的askedBy字段为null。如何用关系填充该字段?

更新

我试图加载会话loadAll(集合)的关系,但它没有帮助。

    final Collection<Question> questions = (Collection<Question>) questionRepository.findQuestionsByUserId(user.getId());
    final Question q = questions.iterator().next();
    System.out.println("After `findQuestionsByUserId`:");
    System.out.println("`q.getTags().size()`: " + q.getTags().size());
    System.out.println("`q.getAskedBy()`: " + q.getAskedBy());
    neo4jOperations.loadAll(questions, 1);
    System.out.println("After `neo4jOperations.loadAll(questions, 1)`:");
    System.out.println("`q.getTags().size()`: " + q.getTags().size());
    System.out.println("`q.getAskedBy()`: " + q.getAskedBy());
    final Collection<AskedBy> askedByCollection = neo4jOperations.loadAll(AskedBy.class);
    System.out.println("`askedByCollection.size()`: " + askedByCollection.size());

以上代码段输出

findQuestionsByUserId之后:
q.getTags().size():0
q.getAskedBy():null
neo4jOperations.loadAll(questions, 1)之后:
q.getTags().size():1 q.getAskedBy():null
askedByCollection.size():0

因此,自定义查询的默认深度似乎为0,由于某些未知原因,我无法加载关系实体。

图表看起来没问题: graph

1 个答案:

答案 0 :(得分:1)

目前,自定义查询不支持深度参数(它在路线图上),因此您有以下选项 -

a)使用repository.findOne(userId)(默认值为深度1,因此应加载AskedBy)。或者使用repository.findOne(userId,depth)自定义深度。或者使用Neo4jTemplate.load(type,id,depth)

b)如果您需要查询超过ID的内容,请使用loadAll上接受一组org.neo4j.ogm.session.Session的{​​{1}}方法。 MusicIntegrationTest

中提供的示例

c)继续自定义查询,但在获得实体ID后,通过提供自定义深度的org.neo4j.ogm.cypher.Filter方法加载它。

相关问题