gpt4 book ai didi

java - Spring 和neo4j : Previous relationships are deleted when adding new ones

转载 作者:行者123 更新时间:2023-12-02 13:19:21 24 4
gpt4 key购买 nike

这是我的用户类:

@NodeEntity
public class User {

@GraphId
private Long id;

@Property
private String name;

@Property
private String email;

@Property
private String password;

@Property
private String photoLink;

@Property
private Integer age;

@Property
private Country country;

@Property
private Gender gender;

@Property
private String about;

@Property
private boolean online;

private Collection<UserHasLanguage> hasLanguage;

@Relationship(type="HAS_ROLE", direction=Relationship.OUTGOING)
private Collection<Role> roles;

@Relationship(type="HAS_IN_FRIENDLIST", direction=Relationship.OUTGOING)
private Collection<User> friendList;

@Relationship(type="HAS_IN_BLACKLIST", direction=Relationship.OUTGOING)
private Collection<User> blackList;

因此,我希望用户与其他用户建立单方关系 HAS_IN_FRIENDLIST。在服务级别,我有一种向用户添加 friend 的方法:

public void addToFriendList (User whoAdds, User whoIsAdded)
throws UserNotFoundException{
if (whoIsAdded == null)
throw new UserNotFoundException("User not found");
Collection<User> friendList = whoAdds.getFriendList();
if (friendList == null)
friendList = new HashSet<>();
friendList.add(whoIsAdded);
whoAdds.setFriendList(friendList);
userRepository.save(whoAdds);
}

但是,当我使用此方法时,该用户以前的一些关系“HAS_IN_FRIENDLIST”被删除。我注意到,whoAdds.getFriendList() 方法始终仅返回 1 个用户。

我该如何解决这个问题?

最佳答案

我无法对此进行测试,但我的理解是不支持 Collection。对于列表,您必须使用其中之一(如指定 here )

java.util.Vector

java.util.List,由 java.util.ArrayList 支持

java.util.SortedSet,由 java.util.TreeSet 支持

java.util.Set,由 java.util.HashSet 支持

数组

所以改变<code>Collection<User></code><code>Set<User></code>

关于java - Spring 和neo4j : Previous relationships are deleted when adding new ones,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43631131/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com