JPA filtering entities from a mapped association

安稳与你 提交于 2021-01-29 21:22:03

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!