Spring Data Neo4J - 创建与现有节点有关系的新节点

Xav*_*ens 5 neo4j spring-data-neo4j spring-data-neo4j-4

我需要创建一个A类的新节点,它与User节点有关系:

@NodeEntity
public class A implements Serializable {

    /**
     * Graph node identifier
     */
    @GraphId
    private Long nodeId;

    /**
     * Enity identifier
     */
    private String id;

    //... more properties ...

    @Relationship(type = "HAS_ADVERTISER", direction = Relationship.OUTGOING)
    private User user;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof IdentifiableEntity)) return false;

        IdentifiableEntity entity = (IdentifiableEntity) o;
        if (!super.equals(o)) return false;

        if (id != null) {
            if (!id.equals(entity.id)) return false;
        } else {
            if (entity.id != null) return false;
        }

        return true;
    }


}


@NodeEntity
public class User implements Serializable {

    /**
     * Graph node identifier
     */
    @GraphId
    private Long nodeId;

    /**
     * Enity identifier
     */
    private String id;

    //... more properties ...


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof IdentifiableEntity)) return false;

        IdentifiableEntity entity = (IdentifiableEntity) o;
        if (!super.equals(o)) return false;

        if (id != null) {
            if (!id.equals(entity.id)) return false;
        } else {
            if (entity.id != null) return false;
        }

        return true;
    }

}
Run Code Online (Sandbox Code Playgroud)

现在,假设我们有新节点 A 的以下数据:

{
  "id": 1,
  "nodeId": "0001-0001",
  "user": {
           "id": 4,
           "nodeId": "0002-0002",
           "name": null,
           "firstName": null
          }
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试创建节点 A,其中新节点 A 与具有“id”:4 和“nodeId”:“0002-0002”(唯一节点标识符)的用户的(已存在)节点之间的关系,但用户节点并未将其更新为“name”和“firstName”字段null

我使用 GraphRepository 代理来创建它:

@Repository
public interface ARepository extends GraphRepository<A> {


}
Run Code Online (Sandbox Code Playgroud)

有没有什么方法可以不进行此更新,仅与 User 节点建立关系?

Lua*_*nne 2

不,您必须按 id 重新加载实体以填充所有缺失值然后保存,或者编写自定义查询。