I have a simple User entity and would like to implement a ManyToMany relation with another users. The class looks like this:
@Entity
public class User {
@Id
@GeneratedValue
private long id;
private String username;
@ManyToMany
private List<User> friends;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public List<User> getFriends() {
return friends;
}
public void setFriends(List<User> friends) {
this.friends = friends;
}
public void addFriend(User user){
if (friends==null) friends = new ArrayList<User>();
friends.add(user);
}
}
This doesn't work how I want however, because I'd like to have only one row in the database for bidirectional relation, in this case it makes two rows, in both directions.
How can I achieve that?
Aucun commentaire:
Enregistrer un commentaire