如何获取@ManyToMany 关系中不存在的所有元素
How to get all elements not present in a @ManyToMany Relationship
我正在尝试获取 UserAccount 不作为管理员或关注者出现的所有频道(-> 不在 subscribedUsers 和管理员集合中)。
这些是我的实体:
public class UserAccount {
@Id ...
private long userId;
@JsonIgnore
@JoinTable(
name="user_channel_sub",
joinColumns = @JoinColumn(name
="user_channel_id",referencedColumnName="userId"),
inverseJoinColumns = @JoinColumn(name =
"channel_user_id",referencedColumnName = "channelId"))
@ManyToMany
private Collection<Channel> subChannel = new ArrayList<Channel>();
@JoinTable(
name="user_channel_admin",
joinColumns = @JoinColumn(name =
"user_channel_id",referencedColumnName= "userId"),
inverseJoinColumns = @JoinColumn(name =
"channel_user_id",referencedColumnName = "channelId"))
@ManyToMany
private Collection<Channel> adminChannel = new ArrayList<Channel>();
....
}
public class Channel {
@Id
private channelId;
@ManyToMany(mappedBy="adminChannel")
private Collection<UserAccount> admins = new ArrayList<UserAccount>();
@ManyToMany(mappedBy = "subChannel")
private Collection<UserAccount> subscribedUsers = new ArrayList<UserAccount>();
....
}
存储库是一个 PagingAndSortingRespository。
我通过这种方法获得了用户是关注者或管理员的所有频道(我真的不需要它,只是想确保至少有一些东西有效):
Collection<Channel> findBySubscribedUsers_UserName_OrAdmins_UserName(String uName1,String uName2);
现在,当我尝试相反的操作时,我得到了重复的数据,是的,基本上是错误的数据:
Collection <Channel> findBySubscribedUsersUserNameNotAndAdminsUserNameNot(String uName1,String uName2);
我做错了什么?这种方法是否可行,还是我需要使用更高级的 spring jpa 查询方法?
可能与 JPA 有关,请参阅文档
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-creation
使用findDistinct
方法和notContaining
。
Collection<Channel> findDistinctByAdminsNotContainingAndSubscribedUsersNotContaining(UserAccount admin, UserAccount subscribedUser);
我正在尝试获取 UserAccount 不作为管理员或关注者出现的所有频道(-> 不在 subscribedUsers 和管理员集合中)。
这些是我的实体:
public class UserAccount {
@Id ...
private long userId;
@JsonIgnore
@JoinTable(
name="user_channel_sub",
joinColumns = @JoinColumn(name
="user_channel_id",referencedColumnName="userId"),
inverseJoinColumns = @JoinColumn(name =
"channel_user_id",referencedColumnName = "channelId"))
@ManyToMany
private Collection<Channel> subChannel = new ArrayList<Channel>();
@JoinTable(
name="user_channel_admin",
joinColumns = @JoinColumn(name =
"user_channel_id",referencedColumnName= "userId"),
inverseJoinColumns = @JoinColumn(name =
"channel_user_id",referencedColumnName = "channelId"))
@ManyToMany
private Collection<Channel> adminChannel = new ArrayList<Channel>();
....
}
public class Channel {
@Id
private channelId;
@ManyToMany(mappedBy="adminChannel")
private Collection<UserAccount> admins = new ArrayList<UserAccount>();
@ManyToMany(mappedBy = "subChannel")
private Collection<UserAccount> subscribedUsers = new ArrayList<UserAccount>();
....
}
存储库是一个 PagingAndSortingRespository。
我通过这种方法获得了用户是关注者或管理员的所有频道(我真的不需要它,只是想确保至少有一些东西有效):
Collection<Channel> findBySubscribedUsers_UserName_OrAdmins_UserName(String uName1,String uName2);
现在,当我尝试相反的操作时,我得到了重复的数据,是的,基本上是错误的数据:
Collection <Channel> findBySubscribedUsersUserNameNotAndAdminsUserNameNot(String uName1,String uName2);
我做错了什么?这种方法是否可行,还是我需要使用更高级的 spring jpa 查询方法?
可能与 JPA 有关,请参阅文档 https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-creation
使用findDistinct
方法和notContaining
。
Collection<Channel> findDistinctByAdminsNotContainingAndSubscribedUsersNotContaining(UserAccount admin, UserAccount subscribedUser);