Spring & neo4j: 添加新关系时删除以前的关系
Spring & neo4j: Previous relationships are deleted when adding new ones
这是我的用户 class:
@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。
在服务级别,我有一个向用户添加朋友的方法:
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() 方法总是 returns 只有 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>Set<User>
这是我的用户 class:
@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。 在服务级别,我有一个向用户添加朋友的方法:
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() 方法总是 returns 只有 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>Set<User>