caribbean
caribbean

Reputation: 317

JPA filtering entities from a mapped association

My friendships sql table has column friend_1, friend_2, status. I have the following mappings in my (snippet below) User class. Currently user.getInitiatedFriendships returns list of Friendships where value of column friend_1 is equal to ID of the user I'm calling this getter on. user.getInvitedToFriendships() returns Friendships where column friend_2 is equal to ID of this user.

I want to know if it is possible to add something to this line @OneToMany(mappedBy = "invitingUser" +>some check if status=0<) to also perform a check on column status and only return those Friendships where status = 0 ?

Thanks

@Entity
@Table(name="users")
public class User {

        ...

    @OneToMany(mappedBy = "invitingUser")
    List<Friendship> initiatedFriendships;

    @OneToMany(mappedBy = "invitedUser"})
    List<Friendship> invitedToFriendships;

        ....

Upvotes: 0

Views: 145

Answers (1)

caribbean
caribbean

Reputation: 317

Sorry, I came up for another way to search for it in google and found it. This is the answer:

    @OneToMany(mappedBy = "invitingUser")
    @Where(clause = "status = 0")
    List<Friendship> initiatedFriendships;

Upvotes: 1

Related Questions