Just a quick question:
There is an entity (for example, a User) that is associated with the relationship of ManyToMany to the same object (for example, this relationship describes "friendship" and it is symmetrical).
What is the fastest way in terms of runtime to check if User A is a “friend” of User B? A “dumb” way would be to get the whole list and then check if the user exists there, but obviously overhead.
I am using JPA 2
Here is an example code:
@Entity
@Table(name="users")
public class UserEntity {
@ManyToMany(fetch = FetchType.LAZY)
private List<UserEntity> friends;
....
}
Juriy source
share