简体   繁体   English

如何使用MERGE保存内部对象

[英]How to save inner objects with MERGE

For now I am using save method from GraphRepository and I also have constraint on user id, it is unique . 现在,我正在使用GraphRepository中的 save方法,并且我对用户ID也有约束,它是unique User have List of users - friends. 用户有用户列表-朋友。 My flow: 我的流程:

  1. Read from db, if user is null save his with friends, I am using save method from GraphRepository 从数据库读取,如果用户为空,则将其与朋友保存,我正在使用GraphRepository中的 save方法
  2. But if user is present i just need to update him and update his friends. 但是如果用户在场,我只需要更新他并更新他的朋友即可。 I am using custom query into DAO method 我在DAO方法中使用自定义查询

     @Query("MERGE (u:User {id:{user}.id}) " + "ON CREATE SET u.id = {user}.id, u.firstName={user}.firstName, u.lastName={user}.lastName, u.imgUrl = {user}.imgUrl " + "ON MATCH SET u.id += {user}.id, u.firstName+={user}.firstName, u.lastName+={user}.lastName, u.imgUrl += {user}.imgUrl") void mergeUser(@Param("user") User user); 

    But with merge method i can't add all user's friends and ON MATCH case doesn't work too 但是使用合并方法时,我无法添加所有用户的朋友,并且在匹配情况下也不起作用 在此处输入图片说明

You probably want to use a Cypher query that looks something like this: 您可能想使用看起来像这样的Cypher查询:

MERGE (u:User {id:{user}.id})
SET u.firstName = {user}.firstName, u.lastName = {user}.lastName, u.imgUrl = {user}.imgUrl
WITH u
UNWIND {friends} AS friend
MERGE (f:User {id:{friend}.id})
SET f.firstName = {friend}.firstName, f.lastName = {friend}.lastName, f.imgUrl = {friend}.imgUrl
MERGE (u)-[:FRIEND_OF]->(f);

There is no need to use ON CREATE or ON MATCH , since you always want to do the same thing either way. 无需使用ON CREATEON MATCH ,因为您总是想以任何一种方式执行相同的操作。 Also, there is no need to explicitly assign to the id property, since MERGE will guarantee that the node will have the desired id value. 此外,由于MERGE将保证节点具有所需的id值,因此无需显式分配给id属性。

I assume that friends collection consists of maps that have the same keys as your user map. 我假设friends集合由与您的user地图具有相同键的地图组成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM