Spring Data Neo4j 4.0.0:无法在具有相同标签的节点之间创建关系

时间:2015-05-23 22:34:12

标签: java neo4j nodes relationship spring-data-neo4j-4

我正在使用Spring Data Neo4j 4.0.0和Neo4j 2.2.1而我正在尝试使用完全相同的标签在两个节点之间创建关系。

所以,我有一个NodeEntity类,我有一个变量,里面有与类本身相同的Type,并将其注释为Relationship。 但是,当我使用存储库对象的save()方法将对象保存到数据库时,无法创建关系。

提前感谢您,我们非常感谢您的建议!

修改

这是节点实体类

public class ArchitectureUnitState extends UnitState {

    public ArchitectureUnitState()
    {
        super();
    }

    public ArchitectureUnitState(String name, String description, String parentArchitectureUnitName)
    {
        super(name, description);
        this.parentArchitectureUnitName = parentArchitectureUnitName;
    }

    @Relationship(type="PART_OF", direction = Relationship.OUTGOING)
    private ArchitectureUnitState architectureUnitState;

    @Relationship(type="STATE_OF", direction = Relationship.OUTGOING)
    private ArchitectureUnit architectureUnit;

    @Transient
    private String parentArchitectureUnitName;

    public void partOf(ArchitectureUnitState architectureUnitState) {
        this.architectureUnitState = architectureUnitState;
    }

    public void stateOf(ArchitectureUnit architectureUnit) {
        this.architectureUnit = architectureUnit;
    }

    public void childOf(String parentArchitectureUnitName) {
        this.parentArchitectureUnitName = parentArchitectureUnitName;
    }

    public String getParentName() {
        return parentArchitectureUnitName;
    }
}

@NodeEntity
public class UnitState {
    @GraphId
    protected Long id;

    private String name;
    private String description;

    public UnitState() {

    }

    public UnitState(String name, String description) {
        this.name = name;
        this.description = description;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }
}

因此,顺序是:我创建了ArchitectureUnitState对象,将它们映射到另一个,然后使用ArchitectureUnitStateRepository的save()方法进行保存。

如果我这样做,则不会创建PART_OF关系,尽管我在调试中看到值存在。

我现在的解决方法是首先保存所有ArchitectureUnitState节点,再从数据库中检索它们,将它们映射到另一个节点,然后再次保存。这样,可以创建关系,但我需要保存两次。

1 个答案:

答案 0 :(得分:1)

以下是使用上述课程的测试用例。

    @Test
        public void testArchitectureState() {
            ArchitectureUnitState state1 = new ArchitectureUnitState("one","desc one","root");
            ArchitectureUnitState state2 = new ArchitectureUnitState("two","desc two","root");
            ArchitectureUnit unit1 = new ArchitectureUnit("unit1");
            ArchitectureUnit unit2 = new ArchitectureUnit("unit2");
            state1.partOf(state2);
            state1.stateOf(unit1);
            state2.stateOf(unit2);
            architectureUnitStateRepository.save(state1);

            state1 = architectureUnitStateRepository.findByName("one");
            assertEquals("two", state1.getArchitectureUnitState().getName());
            assertEquals("unit1", state1.getArchitectureUnit().getName());

            state2 = architectureUnitStateRepository.findByName("two");
            assertNull(state2.getArchitectureUnitState()); 
            assertEquals("unit2", state2.getArchitectureUnit().getName());

} 

它按预期传递,并且图中创建的节点似乎表示相同。 Relationship one way

请注意assertNull(state2.getArchitectureUnitState());成立,因为关系的方向被指定为OUTGOING。

没有从state2中传出PART_OF关系,因此没有任何关系

如果我将测试更改为

@Test
    public void testArchitectureBothWays() {
        ArchitectureUnitState state1 = new ArchitectureUnitState("one","desc one","root");
        ArchitectureUnitState state2 = new ArchitectureUnitState("two","desc two","root");
        ArchitectureUnit unit1 = new ArchitectureUnit("unit1");
        ArchitectureUnit unit2 = new ArchitectureUnit("unit2");
        state1.partOf(state2);
        state2.partOf(state1);
        state1.stateOf(unit1);
        state2.stateOf(unit2);
        architectureUnitStateRepository.save(state1);

        state1 = architectureUnitStateRepository.findByName("one");
        assertEquals("two", state1.getArchitectureUnitState().getName());
        assertEquals("unit1", state1.getArchitectureUnit().getName());


        state2 = architectureUnitStateRepository.findByName("two");
        assertEquals("one",state2.getArchitectureUnitState().getName());
        assertEquals("unit2", state2.getArchitectureUnit().getName());
    }

然后我们在两个方向都有关系,现在state2与state1有关系。 enter image description here