Spring Data Neo4j returns 子节点为空的节点

Spring Data Neo4j returns node with empty child nodes

我有以下 Spring Data Neo4j 4.2.0.BUILD-SNAPSHOT 实体:

@NodeEntity
public class VoteGroup extends BaseEntity {

    private static final String VOTED_ON = "VOTED_ON";
    private final static String VOTED_FOR = "VOTED_FOR";

    @Relationship(type = VOTED_FOR, direction = Relationship.OUTGOING)
    private Decision decision;

    @Relationship(type = VOTED_ON, direction = Relationship.OUTGOING)
    private Criterion criterion;
...
}

@NodeEntity
public class Decision extends Commentable {

    @Relationship(type = VOTED_FOR, direction = Relationship.INCOMING)
    private Set<VoteGroup> voteGroups = new HashSet<>();
...
}

@NodeEntity
public class Criterion extends Authorable {

    @Relationship(type = VOTED_ON, direction = Relationship.INCOMING)
    private Set<VoteGroup> voteGroups = new HashSet<>();

....
}

存储库:

@Repository
public interface VoteGroupRepository extends GraphRepository<VoteGroup> {

    @Query("MATCH (d:Decision)<-[:VOTED_FOR]-(vg:VoteGroup)-[:VOTED_ON]->(c:Criterion) WHERE id(d) = {decisionId} AND id(c) = {criterionId} RETURN vg")
    VoteGroup getVoteGroupForDecisionOnCriterion(@Param("decisionId") Long decisionId, @Param("criterionId") Long criterionId);

}

我正在使用以下构造函数创建 VoteGroup

public VoteGroup(Decision decision, Criterion criterion, double avgVotesWeight, long totalVotesCount) {
        this.decision = decision;
        decision.addVoteGroup(this);
        this.criterion = criterion;
        criterion.addVoteGroup(this);
        this.avgVotesWeight = avgVotesWeight;
        this.totalVotesCount = totalVotesCount;
    }

但是当我试图找到以前保存的 VoteGroup 时:

VoteGroup voteGroup = getVoteGroupForDecisionOnCriterion(decision.getId(), criterion.getId());

我的 voteGroup.decisionvoteGroup.criterion 是 NULL..

但是如果我在 findOne 方法之后立即调用:

voteGroup = voteGroupRepository.findOne(voteGroup.getId());

voteGroup.decisionvoteGroup.criterion 已正确填充。

我的存储库有什么问题 method/Cypher 以及如何修复它?

查询

@Query("MATCH (d:Decision)<-[:VOTED_FOR]-(vg:VoteGroup)-[:VOTED_ON]->(c:Criterion) WHERE id(d) = {decisionId} AND id(c) = {criterionId} RETURN vg")

return只有 VoteGroup 节点等等,这就是 OGM 可以映射的全部内容。 如果您也想要决策和标准,那么您必须 return 这些节点及其关系。这样的事情应该有效:

@Query("MATCH (d:Decision)<-[for:VOTED_FOR]-(vg:VoteGroup)-[on:VOTED_ON]->(c:Criterion) WHERE id(d) = {decisionId} AND id(c) = {criterionId} RETURN vg,d,for,on,c")

此博客 post 包含更多示例:http://graphaware.com/neo4j/2016/04/06/mapping-query-entities-sdn.html

顺便说一句,您共享的 VoteGroup 构造函数不会被 OGM 使用,您需要一个无参数构造函数。