I am currently developing an application with the API in JavaSpringBoot and I need to add relationships between users (friends). For this I made a many-to-many relation which contains two fields:
CREATE TABLE friend_relation
(
fk_id_friend integer REFERENCES users (id) NOT NULL,
fk_id_user integer REFERENCES users (id) NOT NULL,
PRIMARY KEY (fk_id_friend, fk_id_user)
);
When I connect with a user and add relationships : everything is fine, but if I connect to one of the accounts added as a friend by the other user, there is a StackOverflowError.
I know it's because of the almost identical entries in the database, but I have no idea how to get my entity to be correct. Currently each user must add the other individually, I imagine that I have to make a friend request system but again I am blocked. Do I have to make an "effective" field in my friend_relation table. If so, how do I use it? Should I create a specific entity for this table or leave it in the User entity?
Currently, this is what my user entity looks like:
@Entity
@Table(name = "users")
@Data
@Accessors(chain = true)
public class UserEntity {
@Id
@SequenceGenerator(name = "users_generator", sequenceName = "users_sequence", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "users_generator")
@Column(name = "id")
private Integer id;
[...]
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(
name = "friend_relation",
joinColumns = @JoinColumn(name = "fk_id_user", referencedColumnName = "id", nullable = false),
inverseJoinColumns = @JoinColumn(name = "fk_id_friend", referencedColumnName = "id", nullable = false)
)
private List<UserEntity> friends = new ArrayList<>();
}
When trying to modify my entity to avoid the error:
@ManyToMany
@JoinTable(name="friend_relation",
joinColumns=@JoinColumn(name="fk_id_user"),
inverseJoinColumns=@JoinColumn(name="fk_id_friend")
)
private List<UserEntity> friends;
@ManyToMany
@JoinTable(name="friend_relation",
joinColumns=@JoinColumn(name="fk_id_friend"),
inverseJoinColumns=@JoinColumn(name="fk_id_user")
)
private List<UserEntity> friendOf;
I looked for resources on the internet but I did not find it, I may have searched poorly and I apologize in advance if the answer has already been given.
If you can help me it is with great pleasure, thank you in advance! (And sorry for the Google Translate I preferred not to use my rough English)✌
Ok sorry update, i don't post the stake trace : https://pastebin.com/Ls2qRpU4
It happens when I get my user on the front side. I am trying to connect but I cannot because this error occurs.