JPQL check many-to-many relationship

Posted by Juriy on Stack Overflow See other posts from Stack Overflow or by Juriy
Published on 2010-05-25T11:32:30Z Indexed on 2010/05/25 11:51 UTC
Read the original article Hit count: 670

Filed under:
|
|
|

Just a quick question:

There's the entity (for example User) who is connected with the ManyToMany relationship to the same entity (for example this relation describes "friendship" and it is symmetric).

What is the fastest way in terms of execution time to check if User A is a "friend" of user B? The "dumb" way would be to fetch whole List and then check if user exists there but that's obviously the overhead.

I'm using JPA 2

Here's the sample code:

@Entity
@Table(name="users")
public class UserEntity {
    @ManyToMany(fetch = FetchType.LAZY)
    private List<UserEntity> friends;

    ....
}

© Stack Overflow or respective owner

Related posts about java

Related posts about jpa