1
votes

I'm using Spring Data Neo4j 4.0.0 with Neo4j 2.2.1 and I'm trying to create a relationship between two nodes with the exact same labels.

So, I have a NodeEntity class and I have a variable inside with the same Type as the class itself, and annotate it as Relationship. But, when I save the object to the database using the save() method of the repository object, the relationship can't be created.

Thank you in advance and your suggestion would be really appreciated!

EDIT

Here is the node entity classes

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

So, the sequence is: I created the ArchitectureUnitState objects, map one to another, then save with the save() method of the ArchitectureUnitStateRepository.

If I do like this, the PART_OF relationships aren't created, although I see in the debugging that the values are there.

My workaround right now is I save all the ArchitectureUnitState nodes first, retrieve them again from the database, map one to another, then save it again. This way, the relationships can be created, but I need to save two times.

1
We have tests for a class like this: @NodeEntity(label="A") public static class A extends E { @Relationship(type="EDGE", direction= Relationship.INCOMING) A a; } Please post your entity class and code so we can help youLuanne
@Luanne I have edited my post. Thanks!Peter Sie

1 Answers

1
votes

Here's my test case using your classes above.

    @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());

} 

It does pass as expected, and the nodes created in the graph appear to indicate the same. Relationship one way

Note that assertNull(state2.getArchitectureUnitState()); holds true because the direction of the relation is specified as OUTGOING. There is no outgoing PART_OF relation from state2, so none will be loaded.

If I change the test to

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

then we have a relationship in both directions and now state2 has a relationship to state1. enter image description here