4

我正在创建指向它们之间的实体和关系实体。我将关系实体放在一个实体中,然后保存该实体。这也会自动保存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
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?

4

1 回答 1

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

如果您使用动态关系,不确定为什么它不起作用。

于 2013-08-28T22:17:03.893 回答