【问题标题】:Spring Data Neo4j not populating RelationshipEntitySpring Data Neo4j 不填充关系实体
【发布时间】:2013-08-28 20:53:11
【问题描述】:

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

【问题讨论】:

    标签: java neo4j persistence entity-relationship spring-data-neo4j


    【解决方案1】:

    问题出在下面一行

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

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

    【讨论】:

    • 你说得对,谢谢。不幸的是,这给我带来了另一个问题。我不想拥有默认类型(或者我希望能够像我尝试的那样更改它),因为我希望动态设置集合中的每个关系。没有这个,我觉得 Spring Data Neo4j 非常有限。任何人有任何解决方法的想法?
    猜你喜欢
    • 2020-04-12
    • 1970-01-01
    • 2021-06-20
    • 1970-01-01
    • 2021-05-22
    • 1970-01-01
    • 1970-01-01
    • 2016-11-29
    • 1970-01-01
    相关资源
    最近更新 更多