For example I have a user entity in hibernate with these 2 fields:
@Entity
@Table(name = "user")
public class User {
@OneToMany(fetch = FetchType.LAZY)
@JsonIgnore
private Set<User> addedUsers;
@OneToMany(fetch = FetchType.LAZY)
@JsonIgnore
private Set<User> wantToAdd;
}
addedUsers is for 2 people who agrees to match with each other, while wantToAdd is for an user who sends a friend request to another user but didn't receive a response yet.
Here's a series of queries I used: (assume there are 3 users a,b,c)
a add b -> good
a add c -> good
b add c -> failed
Now it gives
"java.sql.SQLIntegrityConstraintViolationException: Duplicate entry '1' for key 'UK_4l1fmdm138stci88r2ht2ggff'" error.
Table in MySQL workbench
It seems like want_to_match_id doesn't allow duplicate. How to fix this problem?
I also think it's bad for user reference to itself but I don't know how I could do otherwise.