3

要使用 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>

在我的应用程序中,我想管理家庭。Persons 作为 NodeEntities,relationtypes 作为 NodeEntities,family-relationships 作为RelationshipEntities。

为了保存节点或关系,我使用repository.save(T t) (repository extends GraphRepository<T>). 这适用于所有节点,但不适用于关系。

显式不起作用的代码:

    Relation createdRelation = new Relation(typeName, from, to, getCurrentUsername());
    createdRelation.setBegin(begin);
    createdRelation.setEnd(end);
    Relation relation = relationRepository.save(createdRelation);

我从 save(T t) 得到一个关系对象。但是,RelationshipEntity 并没有保存在图形数据库中。我的 Relation-Object 也没有任何 id。

关系实体类如下所示:

@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...}

graph-id 保存在 BaseClass 中:

public abstract class BaseGraphEntity implements AuditEntity {

@GraphId
private Long id;

...with getters and setters...}

我现在的问题是:

如何使用 Spring Data Neo4j 4 RC1 保存我的关系实体?

关系实体是否有其他存储库?

PS:我试图将我的图形ID的位置更改为主要的RelationshipEntity,但它不起作用。

4

3 回答 3

2

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

  • @StartNode在实体上设置关系
  • 保存@StartNode实体 OR @RelationshipEntity,似乎关键是对象必须设置在第@StartNode一个

因此,在您的示例中,您必须执行以下操作:

Relation createdRelation = new Relation(typeName, from, to, getCurrentUsername());
createdRelation.setBegin(begin);
createdRelation.setEnd(end);
begin.setRelation(createdRelation);
Relation relation = relationRepository.save(createdRelation);

话虽如此,我不得不承认,我不是 100% 确定这是否是它应该做的方式,因为在当前的文档修订中尚不清楚,但它似乎确实是 SDN4 示例测试中采用的方法: https ://github.com/spring-projects/spring-data-neo4j/blob/4.0.x/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/examples/movies/MoviesIntegrationTest .java(见findOneShouldConsiderTheEntityType

于 2015-07-21T14:36:52.707 回答
2

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

Relation createdRelation = new Relation(typeName, from, to, getCurrentUsername());
    createdRelation.setBegin(begin);
    createdRelation.setEnd(end);
    Relation relation = relationRepository.save(createdRelation);

这里,关系实体正确地表示了与开始节点和结束节点的关系。现在,如果您尝试将此“对象图”导航到起始实体,即begin您会发现您无法通过关系导航到最终实体end

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

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

如果您将对象视为图形,那么这一切都会到位。

于 2015-07-21T15:34:41.930 回答
0

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

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

...我将我的代码更改为

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

因为Luannes的评论,也谢谢你!

于 2015-07-21T14:46:40.823 回答