如何合并而不是使用Neo4jTemplate创建 [英] How To Merge Instead of Create With Neo4jTemplate

查看:140
本文介绍了如何合并而不是使用Neo4jTemplate创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在遍历一个数组,其中每个索引包含两个节点和一个关系(Part_1-> Part_2),并且我正在使用Neo4jTemplate.save()方法将其持久化到数据库中.但是,某些索引具有重复的节点,这些节点将与其他节点具有关系(Part_2-> Part_3).我的版本当前每次都创建一个新的节点和关系,而不是合并(如果已经存在一个节点).

I'm currently iterating over an array where each index contains two nodes and a relationship (Part_1 -> Part_2), and I'm using the Neo4jTemplate.save() method to persist it into the database. However, some indexes have repeated nodes that will have relationships with other nodes (Part_2 -> Part_3). My version currently creates a new node and relationship every time instead of merging if a node already exists.

我阅读了此 ,但我不知道如何实现它,以便两个相同的节点具有相同的ID.我当前的代码是这样的:

I read this post, but I don't understand how to implement it so that two of the same nodes will have the same ID. My current code works like this:

  1. 创建两个节点
  2. 建立他们的关系
  3. 向节点添加关系
  4. 使用Neo4jTemplate.save()坚持使用它
  1. Create two nodes
  2. Create their relationship
  3. Add relashionship to the node
  4. Persist it using Neo4jTemplate.save()

我需要将什么更改为MERGE而不是CREATE?我要在保留之前进行检查还是在SDN 4中保留进行检查?

What do I need to change to MERGE instead of CREATE? Do I need to do a check before I persist or is there a way to check while persisting in SDN 4?

我决定使用Neo4jTemplate.query()方法编写Cyper查询,但是我不知道如何正确创建参数以合并多个节点.我可以为一个节点正确地创建一个MERGE语句:

I've decided to use the Neo4jTemplate.query() method to write Cyper queries, however I don't know how to create the parameters correctly for merging multiple nodes. I can create a MERGE statement correctly like this for one node:

Map< String,Object> params = new HashMap< String,Object>();
Map< String,Object>节点=新的HashMap< String,Object>();

Map<String, Object> params = new HashMap<String, Object>();
Map<String, Object> node = new HashMap<String, Object>();

node.put("name","Part_1");
params.put("props",node_1);
字符串查询="MERGE(n1:Part {name:{props} .name})";

node.put("name", "Part_1");
params.put("props", node_1);
String query = "MERGE( n1:Part {name:{props}.name} )";

template.query(query,params);

template.query(query, params);

我的目标是在两个节点上调用合并,然后再次调用合并以在一条语句中创建关系.我的代码现在看起来像这样:

My goal is to call merge on two nodes and then call merge again to create the relationship in one statement. My code right now looks like this:

Map< String,Object> params = new HashMap< String,Object>();
List< Map< String,Object>>地图;
Map< String,Object> node1 =新的HashMap< String,Object>();
Map< String,Object> node2 =新的HashMap< String,Object>();

Map<String, Object> params = new HashMap<String, Object>();
List<Map<String, Object>> maps;
Map<String, Object> node1 = new HashMap<String, Object>();
Map<String, Object> node2 = new HashMap<String, Object>();

node1.put("name1","Part_1");
node2.put("name2","Part_2");
maps = Arrays.asList(node_1,node_2)

node1.put("name1", "Part_1");
node2.put("name2", "Part_2");
maps = Arrays.asList(node_1, node_2)

params.put("props",地图);
字符串查询="MERGE(n1:Part {name:{props} .name1})
MERGE(n2:Part {name:{props} .name2)
MERGE(n1)-[:CREATED]->(n2);

params.put("props", maps);
String query = "MERGE( n1:Part {name:{props}.name1} )
MERGE( n2:Part {name:{props}.name2 )
MERGE(n1)-[:CREATED]->(n2)";

template.query(query,params);

template.query(query, params);

到目前为止,我已经看到所有示例在参数中包含多个节点时,只需在调用时遍历整个对象即可.我还没有找到带有参数的示例,可以在其中指定要引用的特定节点.如何在引用特定节点的地方创建参数?预先感谢!

All the examples I've seen so far with multiple nodes in the parameters simply just iterate through the whole thing when called. I haven't found any examples that have parameters where you can specify the certain node in it that you're referring to. How do I create a parameter where I refer to a certain node? Thanks in advance!

推荐答案

很难在不看到代码的情况下提出更改建议,但是听起来就像您每次都保存新的实体实例一样.

It's hard to suggest what to change without seeing your code, but it sounds like you're saving new entity instances every time.

传递给Neo4jTemplate.save(entity)的实体一旦写入数据库,应使用ID更新. SDN将尝试更新已经具有ID的节点,因此我建议重用实体实例,而不是创建新的实例.

The entity passed to Neo4jTemplate.save(entity) should be updated with an ID once it's been written to the database. SDN will try to update nodes that already have an ID so I'd suggest re-using entity instances instead of creating new ones.

如果您不想自己管理这些实体,则可以尝试在步骤1中加载实体,然后仅在尚不存在的情况下创建它们.

If you don't want to manage these entities yourself, you could attempt to load entities in step 1 and then only create them if they don't already exist.

这篇关于如何合并而不是使用Neo4jTemplate创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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