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

时间:2016-05-24 15:11:38

标签: neo4j cypher spring-data-neo4j spring-data-neo4j-4

我从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);
    }
为了创建ONE_TO_MANY关联,

对于SDN 4是不够的。我还要添加其他行以便将对象添加到父集合中。

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);
    }

我做错了什么?

1 个答案:

答案 0 :(得分:0)

遇到同样的问题,请尝试从DEFINED_BY @Relationship

中删除Criterion
   @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