4

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?

sparkyspider
  • 13,195
  • 10
  • 89
  • 133
Rob McFeely
  • 2,823
  • 8
  • 33
  • 50

1 Answers1

2

The issue is with the below line

@RelatedToVia(type="some default type", direction = Direction.BOTH)
Set<ThingRelationship> relationships = new HashSet<ThingRelationship>();

Changing that to below works.

@RelatedToVia(type="REALLY_REALLY_LIKES", direction = Direction.BOTH)
Set<ThingRelationship> relationships = new HashSet<ThingRelationship>();

Not sure why it doesn't work if you are using dynamic relationships.

Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327
  • You're right thanks. Unfortunately this presents me with another problem. I don't want to have a default type (or I want to be able to change it like I was trying) as I want each relationship in the set to be set dynamically. Without this I feel that Spring Data Neo4j is quite limited. Anyone got any ideas of a work around? – Rob McFeely Aug 29 '13 at 08:19