Spring Data Neo4j 4 Repository 方法返回 1 个元素



我要从SDN 3迁移到SDN 4,从Neo4j 2.3迁移到3.0.1

在新依赖项上,我的测试因断言而失败。它只返回一个节点,而不是 3 个节点。

@NodeEntity
public class Decision extends Commentable {
    private final static String CONTAINS = "CONTAINS";
    private final static String DEFINED_BY = "DEFINED_BY";
    private final static String VOTED_FOR = "VOTED_FOR";
    private String name;
    @Relationship(type = DEFINED_BY, direction = Relationship.INCOMING)
    private Set<Criterion> criteria = new HashSet<>();
....

@NodeEntity
public class Criterion extends Authorable {
    private final static String CONTAINS = "CONTAINS";
    private final static String DEFINED_BY = "DEFINED_BY";
    private String name;
    private String description;
    @Relationship(type = CONTAINS, direction = Relationship.INCOMING)
    private CriterionGroup group;
    @Relationship(type = DEFINED_BY, direction = Relationship.OUTGOING)
    private Decision owner;

@Test
    public void testGetChildDecisionsSortedBySumOfCriteriaAvgVotesWeightWithCoefficients() {
        User user = userService.createUser("test", "test", "test@test.com", null, null);
        final Decision rootDecision1 = decisionDao.create("Root decision1", "Root decision 1 description", null, user);
        final Criterion rootDecision1Criterion1 = criterionDao.create("rootDecision1Criterion1", "rootDecision1Criterion1", rootDecision1, user);
        final Criterion rootDecision1Criterion2 = criterionDao.create("rootDecision1Criterion2", "rootDecision1Criterion2", rootDecision1, user);
        final Criterion rootDecision1Criterion3 = criterionDao.create("rootDecision1Criterion3", "rootDecision1Criterion3", rootDecision1, user);
        assertEquals(3, criterionDao.getCriteriaDefinedByDecision(rootDecision1.getId()).size());

存储库方法:

@Query("MATCH (d:Decision)<-[:DEFINED_BY]-(c:Criterion) WHERE id(d) = {decisionId} RETURN c")
List<Criterion> getCriteriaDefinedByDecision(@Param("decisionId") Long decisionId);

此问题的原因是什么以及如何解决?

更新

我发现建筑

public Criterion(String name, String description, Decision owner, User author) {
        this.name = name;
        this.description = description;
        this.owner = owner;
        setAuthor(author);
    }

对于SDN 4来说,不足以创建ONE_TO_MANY关联。我必须添加额外的行才能将对象添加到父集合中。

public Criterion(String name, String description, Decision owner, User author) {
        this.name = name;
        this.description = description;
        this.owner = owner;
        if (owner != null) {
            owner.addCriterion(this);
        }
        setAuthor(author);
    }

我做错了什么?

遇到同样的问题,请尝试从Criterion中删除DEFINED_BY @Relationship

   @NodeEntity
   public class Criterion extends Authorable {
    private final static String CONTAINS = "CONTAINS";
    private final static String DEFINED_BY = "DEFINED_BY";
    private String name;
    private String description;
    @Relationship(type = CONTAINS, direction = Relationship.INCOMING)
    private CriterionGroup group;

}

首先创建Criterion,然后将它们连接到Decision

最新更新