删除 Spring Data Neo4j 中的简单关系 [英] Removing simple Relationships in Spring Data Neo4j

查看:64
本文介绍了删除 Spring Data Neo4j 中的简单关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

晚上好,

我正在使用 SDN 3,并且在删除基础图中的简单关系 (RelateTo) 时遇到问题.场景是我想在我的 Web 应用程序中的用户之间建立一个好友请求/批准系统.我通过在用户之间创建HAS_REQUESTED"关系来发出请求没有问题.但是一旦收到好友请求的用户点击批准",就会建立FRIENDS_WITH"关系,而没有正确删除HAS_REQUESTED"关系.下面的代码演示了整个过程:

I am using SDN 3, and am running into problems with removing simple relationships (RelateTo) in my underlying graph. The scenario is that I want to establish a Friend request/approval system amongst Users in my web application. I have no problem issuing the request by creating a "HAS_REQUESTED" relationship between Users. but once the User receiving the friend request hits "approve", the "FRIENDS_WITH" relationship is established without properly removing the "HAS_REQUESTED" relationship. the code below walks through the process:

相关的Controller方法

The relevant Controller method

@RequestMapping(value="/approve/friend/{friendId}")
public String approveFriend(@PathVariable("friendId") String friendId){
    User me = userService.findByEmail(userService.getAuthenticatedUser().getName());
    userService.removeOldRequests(friendId, me);
    userService.approveFriendship(friendId, me);
    return "redirect:/friends";
}

有问题的 UserService 方法.me"是最初向friendId/friend"发送好友请求的经过身份验证的用户:

The UserService method in question. 'me' is the authenticated user who originally sent the friend request to 'friendId/friend':

public void removeOldRequests(String friendId, User me){
try{
    User friend = userRepository.findByUserId(friendId);
    friend.addStartNodeForUsersRequestingMe(me, false);
    template.save(friend);
}catch(Exception e){
    e.printStackTrace();
}

这是我的用户实体节点(不包括不相关的字段/getter/setter.)

and here is my User entity Node (excluding unrelated fields/getters/setters.)

@NodeEntity

公共类用户{

@GraphId Long nodeId;
@Indexed
String userId;
String username;
String firstName;
String lastName;
String email;
String aboutMe;
String Quote;
String favoriteBook;
int age;
Date userCreation;
String sex;
String password;
Role role;
byte[] picture;


@RelatedTo(type="FRIENDS_WITH", direction=Direction.BOTH)
@Fetch
Set<User> friends;


@RelatedTo(type="HAS_FRIEND_REQUEST")
@Fetch
Set<User> startNodeForUsersRequestingMe;

@RelatedTo(type="HAS_FRIEND_REQUEST", direction=Direction.INCOMING)
@Fetch
Set<User> UsersWhoHaveRequestedMe;


public void addStartNodeForUsersRequestingMe(User user, boolean flag){
    if(flag){
        this.startNodeForUsersRequestingMe.add(user);
    }else{
        this.startNodeForUsersRequestingMe.remove(user);
    }

}
public void addUsersWhoHaveRequestedMe(User user, boolean flag){
    if(flag){
        this.UsersWhoHaveRequestedMe.add(user);
    }else{
        this.UsersWhoHaveRequestedMe.remove(user);
    }

}

我用来返回当前用户的好友请求的存储库方法如下.现在它被配置为只返回用户拥有的任何关系,即HAS_FRIEND_REQUEST",只是为了测试目的,看看我是否可以让用户 A 和用户 B 的一个好友请求不被返回.

The repository method I am using to return the current user's friend requests is below. Right now it is configured to just return any relationship the user has that is "HAS_FRIEND_REQUEST" just for testing purposes to see if I can get User A with one friend request from User B to NOT be returned.

@Query("START user=node({0})"
    +"MATCH (user)-[:HAS_FRIEND_REQUEST]-(friend)"
    + "return friend;")

可迭代的 getUserFriendRequests(User user);

Iterable getUserFriendRequests(User user);

有关如何以干净的方式正确删除HAS_FRIEND_REQUEST"的任何指导将不胜感激.要么是这样,要么是处理好友请求握手"想法的更好方法.如果您对我有任何问题或疑虑,请不要犹豫,将它们提交给我.

Any guidance on how to properly remove the "HAS_FRIEND_REQUEST" in a clean manner would be greatly appreciated. either that, or maybe a better way to handle the "friend request Handshake" idea. If you have any questions or concerns for me, please do not hesitate to bring them to my attention.

推荐答案

您可以从集合中移除目标用户或者使用 Neo4jTemplate 方法删除关系.

You can remove the target user from the collection or use the Neo4jTemplate method to delete the relationship.

template.deleteRelationshipBetween(对象开始,对象结束,字符串类型)

这篇关于删除 Spring Data Neo4j 中的简单关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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