问题
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;
....
回答1:
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;
来源:https://stackoverflow.com/questions/61235419/jpa-filtering-entities-from-a-mapped-association