I have a User
node with FRIEND_REQUEST
relationships mapped to a sentFriendRequestList
list and to a receivedFriendRequestList
list like below:
@Node
data class User(
@Id
@GeneratedValue(UUIDStringGenerator::class)
var userId: String?,
@Relationship(type = "FRIEND_REQUEST", direction = Relationship.Direction.OUTGOING)
var sentFriendRequestList: MutableList<FriendRequest> = mutableListOf(),
@Relationship(type = "FRIEND_REQUEST", direction = Relationship.Direction.INCOMING)
var receivedFriendRequestList: MutableList<FriendRequest> = mutableListOf(),
var email: String
)
The FriendRequest
class:
@RelationshipProperties
data class FriendRequest(
@Id
@GeneratedValue
var friendRequestId: Long?,
/**
* Represents the receiver in an OUTGOING relationship and the sender in an INCOMING relationship.
*/
@TargetNode
var friendRequestOtherNode: User
){
constructor(friendRequestOtherNode: User) : this(null, friendRequestOtherNode)
}
When saving multiple friend requests, on some occasions all previously created relationships disappear from the given nodes and only the newly created relationship appears.
I save like this:
fun saveFriendRequest(sender: User, receiver: User) {
val sentFriendRequest = FriendRequest(receiver)
val receivedFriendRequest = FriendRequest(sender)
sender.sentFriendRequestList.add(sentFriendRequest)
receiver.receivedFriendRequestList.add(receivedFriendRequest)
userRepository.save(sender)
userRepository.save(receiver)
}
I don't understand what the problem is, especially since sometimes it runs without failure.
I created a small test project which is available on my GitHub: link. It contains the data structure and a test class that can be run instantly. The tests show the same problem, after multiple runs it can either fail or be successful.