我正在创建指向它们之间的enties和RelationshipEntity。我将RelationshipEntity放在实体中,然后保存实体。这也会自动保存RelationshipEntity。
我知道关系已经保存,因为我可以单独检索实体和RelationshipEntity。但是,当我检索开始或结束实体时,其关系集为空。
我尝试过强烈的诱惑而没有快乐 谁能看到我在这里做错了什么?
我的实体......
@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;
}
...
}
我的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;
}
最后我的测试....(最终断言失败)
@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
}
最终断言失败,“预期1但发现0”:( 返回的实体是否应该存在RelationsipEntity,因为我急切地想要提取?
答案 0 :(得分:2)
问题在于以下行
@RelatedToVia(type="some default type", direction = Direction.BOTH)
Set<ThingRelationship> relationships = new HashSet<ThingRelationship>();
将其更改为以下作品。
@RelatedToVia(type="REALLY_REALLY_LIKES", direction = Direction.BOTH)
Set<ThingRelationship> relationships = new HashSet<ThingRelationship>();
如果使用动态关系,不确定为什么它不起作用。