使用Spring-Data在Neo4J中创建关系 [英] Creating Relationships in Neo4J using Spring-Data

查看:1022
本文介绍了使用Spring-Data在Neo4J中创建关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在neo4j中创建一个人在其中有一个朋友列表的关系.我可以通过两种方式使用spring-data来做到这一点.

I want to create a relationship in neo4j where a Person has a list of friends. I can do this in two ways using spring-data.

a)创建一个具有列表的类Person,该列表代表朋友,并使用@Relationship对其进行注释.

a) Create a class Person with a List repesenting friends and annotate the same with @Relationship.

@NodeEntity(label="Person")
public class Person {

    @GraphId
    private Long id;
    private String firstName;
    private String lastName;
    private String email;
    @Relationship(type = "FRIEND_WITH")
    List<Person> friends; 
}

b)创建不包含任何列表的Person对象,并使用Cypher来创建"FRIEND_WITH"与

b) Create the Person object without any List and create the relationship of "FRIEND_WITH" with Cypher like

@Query "CREATE (a)-[FRIEND_WITH]->(b)"

这两种方法的优点/缺点是什么?

What are the advantages/disadvanages of both the approaches ?

推荐答案

应尽可能使用代码(而不是查询)在域模型中管理实体和关系.在代码中执行此操作的好处是您的域对象和图形将保持同步. SDN使用的基础对象图映射器不了解您的查询在做什么,因此无法为您对域模型进行任何更改.这意味着,每次使用查询对数据库进行变异时,您都可能需要再次重新加载所有对象.

Wherever possible you should manage entities and relationships in your domain model using code, not queries. The advantage of doing it in code is that your domain objects and the graph will remain in sync. The underlying Object Graph Mapper using by SDN does not understand what your queries are doing and so cannot make any changes to your domain model for you. This means that every time you mutate the database using a query, you will potentially need to reload all your objects again.

这篇关于使用Spring-Data在Neo4J中创建关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆