Inside a Spring Boot server backend I need to create a bidirectional association between two entities (User/Match). A user could have many matches and a match always contains two users.
The problem is that I do not find the correct annotation for the association properties.
The main issue is that there is no update of the user's match list, when I call the likeUser method in the MatchService. I create a new match object and set the back reference from user to match (adding the match to its list of matches) in the user-setter method of match and save the match afterwards. But the manipulation of the users match list wont be saved in the database.
Does anyone know the correct way of solving this problem?
Here is a short extract of my code:
Short explanation to the
likeUser() method in
MatchService class:
likedUser comes from my RestController. If you press "like" on a user at the frontend, the likedUser is send as a json object to the controller. The controller then calls the
matchService.likeUser(likedUser) method. This method in the service layer gets the
currentUser (
initialUser) from the securtiyContext from the
userService. Its checks if
likedUser has already liked the
initialUser. Let's say the
likedUser has not reliked the
initialUser. Then a new match object is created.
The setter-method setUsers(initialUser, likedUser) added the new match to the match list of the respective user.
Afterwards the new created match is saved inside the persistence layer (via a CrudRepository interface) by calling the
matchSerice.addMatch(unconfirmedMatch) method.
The Problem is: When I check the match list of the users, it is still empty, because only the match object is refreshed in the persistence layer.
Hope anyone can help me with this.
Greets, André