Alex
Alex

Reputation: 65

Spring & neo4j: Previous relationships are deleted when adding new ones

Here is my 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;

So I want users to have one-side relationships HAS_IN_FRIENDLIST to other users. At the service level I have a method for adding friends to user:

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);
}

However, when I use this method, some previous relationships "HAS_IN_FRIENDLIST" of this user are removed. I have noticed, that whoAdds.getFriendList() method always returns only 1 User.

How can I fix this?

Upvotes: 0

Views: 84

Answers (1)

Tezra
Tezra

Reputation: 8833

I can't test this, but my understanding is that Collection isn't supported. For a list, you must use one of these (as specified here)

java.util.Vector

java.util.List, backed by a java.util.ArrayList

java.util.SortedSet, backed by a java.util.TreeSet

java.util.Set, backed by a java.util.HashSet

Arrays

So change Collection<User> to Set<User>

Upvotes: 1

Related Questions