I am creating enties and RelationshipEntitys that point between them. I place the RelationshipEntitys within an entity and then save the entity. This also saves the RelationshipEntity automatically.
I know the relationship has saved as I then can retrieve both Entities and RelationshipEntitys individually. However when I retrieve the start or end entity its relationship set is empty.
I have tried forcing eager fetching but no joy.
Can anyone see what I am doing wrong here?
My Entity...
@NodeEntity
public class Thing {
public Thing() {
super();
}
@GraphId
private Long nodeId;
private Long uuid; // unique across domains
@Fetch
@RelatedToVia(type="some default type", direction = Direction.BOTH)
Set<ThingRelationship> relationships = new HashSet<ThingRelationship>();
@Fetch
private Set<Property<?>> properties;
public ThingRelationship relatedTo(Thing thing, String relationshipType){
ThingRelationship thingRelationship = new ThingRelationship(this, thing, relationshipType);
relationships.add(thingRelationship);
return thingRelationship;
}
public Set<ThingRelationship> getRelationships() {
return relationships;
}
...
}
My RelationshipEntity...
@RelationshipEntity
public class ThingRelationship {
public ThingRelationship() {
super();
}
//incremental neo4j set ID
@GraphId Long nodeId;
//Start and end nodes
@StartNode Thing startThing;
@EndNode Thing endThing;
//Relationship Type
@org.springframework.data.neo4j.annotation.RelationshipType
String relationship;
public ThingRelationship(Thing startThing, Thing endThing, String relationship) {
super();
this.startThing = startThing;
this.endThing = endThing;
this.relationship = relationship;
}
ANd Finally My Test.... (fails on final assert)
@Test
@Rollback(false)
public void testAddRelationship(){
Thing thingA = new Thing();
template.save(thingA);
Thing retrievedThingA = template.findOne(thingA.getNodeId(), Thing.class); //returns a thing OK
assertNotNull(retrievedThingA);
Thing thingB = new Thing();
template.save(thingB);
Thing retrievedThingB = template.findOne(thingB.getNodeId(), Thing.class); //returns a thing OK
assertNotNull(retrievedThingB);
//Relationship
ThingRelationship thingRelationship = thingB.relatedTo(thingA, "REALLY_REALLY_LIKES");
template.save(thingRelationship);
ThingRelationship thingRelationshipRetrieved = template.findOne(thingRelationship.getNodeId(), ThingRelationship.class);
assertEquals(thingB.getNodeId(), thingRelationshipRetrieved.getStartThing().getNodeId());
assertEquals(thingA.getNodeId(), thingRelationshipRetrieved.getEndThing().getNodeId());
Thing retrievedThingFinal = template.findOne(thingB.getNodeId(), Thing.class);
template.fetch(retrievedThingFinal.relationships);
assertEquals(1, retrievedThingFinal.getRelationships().size()); //FAILS HERE
}
The final assert fails with "Expected 1 but found 0" :( Should the returned entity not have the RelationsipEntity present since I am eagerly fetching?