|
@@ -186,30 +186,35 @@ public class BaseNodeRepository {
|
|
|
|
|
|
/**
|
|
/**
|
|
* 实体合并
|
|
* 实体合并
|
|
- * @param label
|
|
|
|
- * @param properties
|
|
|
|
- * @return 实体map
|
|
|
|
*/
|
|
*/
|
|
- public Map<String, Object> mergeNode(String label, Map<String, Object> properties) {
|
|
|
|
- StringBuilder createClause = new StringBuilder();
|
|
|
|
- if (CollUtil.isEmpty(properties)) {
|
|
|
|
- properties = new HashMap<>();
|
|
|
|
- }
|
|
|
|
- properties.put("is_deleted","N");
|
|
|
|
- properties.forEach((key, value) -> createClause.append(key).append(": '").append(value).append("'").append(", "));
|
|
|
|
-
|
|
|
|
- // 移除最后的逗号
|
|
|
|
- if (createClause.length() > 0) {
|
|
|
|
- createClause.setLength(createClause.length() - 2);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- String query = String.format("MERGE (n:`%s` { %s }) RETURN n", label, createClause);
|
|
|
|
|
|
+ public Map<String, Object> mergeNode(String firstLabel,Long firstId,String secondLabel,Long secondId,String newName,String newLabel) {
|
|
|
|
+
|
|
|
|
+ String query = String.format("MATCH (a:`%s`), (b:`%s`)\n" +
|
|
|
|
+ "WHERE id(a) = $firstId AND id(b) = $secondId\n" +
|
|
|
|
+ "// 创建一个新的节点 MergedNode 并合并属性\n" +
|
|
|
|
+ "MERGE (m:`%s` {name: $newName})\n" +
|
|
|
|
+ "// 合并节点 A 和节点 B 的所有关系到新的节点\n" +
|
|
|
|
+ "// 从 A 到其他节点的关系\n" +
|
|
|
|
+ "WITH a, b, m\n" +
|
|
|
|
+ "MATCH (a)-[rA]->(target)\n" +
|
|
|
|
+ "MERGE (m)-[rAMerged]->(target)\n" +
|
|
|
|
+ "SET rAMerged = rA\n" +
|
|
|
|
+ "\n" +
|
|
|
|
+ "// 从 B 到其他节点的关系\n" +
|
|
|
|
+ "MATCH (b)-[rB]->(target2)\n" +
|
|
|
|
+ "MERGE (m)-[rBMerged]->(target2)\n" +
|
|
|
|
+ "SET rBMerged = rB\n" +
|
|
|
|
+ "\n" +
|
|
|
|
+ "// 删除原有的两个节点\n" +
|
|
|
|
+ "WITH m, a, b\n" +
|
|
|
|
+ "DETACH DELETE a, b\n" +
|
|
|
|
+ "RETURN m\n", firstLabel, secondLabel, newLabel);
|
|
|
|
|
|
try(Session session = neo4jUtil.getSession()) {
|
|
try(Session session = neo4jUtil.getSession()) {
|
|
- StatementResult result = session.run(query);
|
|
|
|
|
|
+ StatementResult result = session.run(query,Values.parameters("firstId", firstId,"secondId", secondId,"newName",newName));
|
|
Map<String,Object> map = new HashMap<>();
|
|
Map<String,Object> map = new HashMap<>();
|
|
if(result.hasNext()) {
|
|
if(result.hasNext()) {
|
|
- Node node = result.single().get("n").asNode();
|
|
|
|
|
|
+ Node node = result.single().get("m").asNode();
|
|
map.put("id", node.id());
|
|
map.put("id", node.id());
|
|
map.put("labels", node.labels());
|
|
map.put("labels", node.labels());
|
|
map.put("properties", node.asMap());
|
|
map.put("properties", node.asMap());
|