Java SDN 4-RC1:RelationRepository.save(relationshipEntity)don';t在图形中保存关系实体

Java SDN 4-RC1:RelationRepository.save(relationshipEntity)don';t在图形中保存关系实体,java,cypher,spring-data-neo4j,spring-data-neo4j-4,Java,Cypher,Spring Data Neo4j,Spring Data Neo4j 4,要使用neo4j graphdatabase独立服务器,我将SDN 4.0.0.RC1的依赖项添加到pom中: <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-neo4j</artifactId> <version>4.0.0.RC1</version

要使用neo4j graphdatabase独立服务器,我将SDN 4.0.0.RC1的依赖项添加到pom中:

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-neo4j</artifactId>
        <version>4.0.0.RC1</version>
        <exclusions>
            <exclusion>
                <groupId>org.neo4j.app</groupId>
                <artifactId>neo4j-server</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
我从save(T)中得到一个关系对象。 但是RelationshipEntity不会持久保存在graphdatabase中。而且我的关系对象没有任何id

RelationshipEntity类如下所示:

@RelationshipEntity(type = "RELATION")
public class Relation extends BaseMutableGraphEntity {

@Property
private String type;

@StartNode
private Person fromPerson;

@EndNode
private Person toPerson;

private Relation() {
}

...getters and setters...}
图形id保存在基类中:

public abstract class BaseGraphEntity implements AuditEntity {

@GraphId
private Long id;

...with getters and setters...}
我现在的问题是:

如何使用Spring数据Neo4j 4 RC1保存我的RelationshipEntities?

是否有其他RelationshipEntities存储库?


注意:我试图将我的图形id的位置更改为主RelationshipEntity,但它不起作用。

我也遇到了这个怪癖,能够通过以下方式保持我的关系:

  • @StartNode
    实体上设置您的关系
  • 保存
    @StartNode
    实体或
    @RelationshipEntity
    ,关键是必须首先在
    @StartNode
    上设置对象
因此,在您的示例中,您必须执行以下操作:

Relation createdRelation=新关系(typeName、from、to、getCurrentUsername());
createdRelation.setBegin(开始);
createdRelation.setEnd(结束);
begin.setRelation(createdRelation);
关系=relationRepository.save(createdRelation);
所有这些都表明,我必须承认,我不能100%确定这是不是应该这样做,因为当前的文档修订版中不清楚这一点,但这似乎是SDN4示例测试中采用的方法:
(请参见
findOneShouldConsiderTheEntityType

这是解决我问题的代码,谢谢simonl

。。。我把代码改成

Relation relation = new Relation(typeName, from, to, getCurrentUsername());
relation.setBegin(begin);
relation.setEnd(end);
from.addOutgoingRelation(relation);
return createResponseBuilder.build(relationRepository.save(relation));

因为Luanes的评论,也谢谢你

您看到的是,对象图(您的域模型)与您期望的实际图不对应

Relation createdRelation = new Relation(typeName, from, to, getCurrentUsername());
    createdRelation.setBegin(begin);
    createdRelation.setEnd(end);
    Relation relation = relationRepository.save(createdRelation);
这里,关系实体正确地表示与开始节点和结束节点的关系。现在,如果您尝试将此“对象图”导航到开始实体,即
begin
,您将发现无法通过关系导航到结束实体
end

当实体被持久化时,将遍历对象图以确定哪些是新的或修改的,并持久化这些。在这种情况下,当遍历到达起始节点时,它找不到与其他节点的关系,并且实际上,本应创建的关系没有找到

Simon的代码之所以有效,是因为他使实体图与物理图一致。然后,您可以保存末尾的实体或关系实体本身


如果你用图形这样的对象来思考问题,这一切都会到位。

谢谢,这就解决了问题。但不幸的是,我无法使用我的RelationshipRepository。。。我的两个人是“从”和“到”,但我知道你的帖子是什么意思;实际上,我已经对它进行了测试,并且能够获得
RelationshipRepository.save
也可以使用,奇怪的是,您必须首先将您的关系添加到“from”对象中。一旦这样做了,保存关系或“from”对象就应该起作用了。这实际上取决于对象图是否如您在物理图中所期望的那样可导航。我添加了一个解释。“开始”和“结束”是我的关系的可选日期属性。“发件人”和“收件人”是两个关联方。
Relation relation = new Relation(typeName, from, to, getCurrentUsername());
relation.setBegin(begin);
relation.setEnd(end);
from.addOutgoingRelation(relation);
return createResponseBuilder.build(relationRepository.save(relation));
Relation createdRelation = new Relation(typeName, from, to, getCurrentUsername());
    createdRelation.setBegin(begin);
    createdRelation.setEnd(end);
    Relation relation = relationRepository.save(createdRelation);