本文介绍了JPA过滤来自映射关联的实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的友谊sql表具有列friend_1, friend_2, status.我的User类(下面的代码片段)中具有以下映射.当前user.getInitiatedFriendships返回Friendships的列表,其中列friend_1的值等于我正在调用此getter的用户的ID.user.getInvitedToFriendships()返回Friendships,其中列friend_2等于该用户的ID.

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.

我想知道是否有可能在此行@OneToMany(mappedBy = "invitingUser" +>some check if status=0<)中添加一些内容,以便也对列status进行检查,并且只返回那些Friendships其中status = 0吗?

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 ?

谢谢

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

        ...

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

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

        ....

推荐答案

对不起,我想出了另一种方法来在google中搜索它并找到了它.这就是答案:

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;

这篇关于JPA过滤来自映射关联的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 01:31