[英]Spring JPA intersect two list fields of an entity
I have a entity called A
which has one property userGroups
: 我有一个名为A
的实体,它具有一个属性userGroups
:
@entity
Public class A {
@OneToMany
@JoinTable(name = "a_user_groups", ...)
private Set<UserGroup> userGroups;
...
}
I need to find those A
entities which their userGroups
has no intersection with given Set<UserGroup> userGroups
parameter. 我需要找到其userGroups
与给定的Set<UserGroup> userGroups
参数没有交集的那些A
实体。
This is my method: 这是我的方法:
@Query("SELECT a FROM A a WHERE :userGroups intersect a.userGroups is NULL")
List<A> getAWithNoIntersectionInGroups(@param("userGroups") Set<UserGroup> userGroups)
But there is no intersect
keyword in jpa. 但是jpa中没有intersect
关键字。
Not In
keyword not works here: Not In
关键字在这里不起作用:
Suppose there is an A
entity with user groups ids {1,3}
and my passed user group ids are {3,4}
, Now {1,3}
is not in {3,4}
and it's true and entity will be selected, but it shouldn't selected, because there is a mutual item {3}
假设有一个用户组ID为{1,3}
的A
实体,我传递的用户组ID为{3,4}
,现在{1,3}
不在{3,4}
,它是true,将选择实体,但不应选择它,因为存在相互的项目{3}
You can use NOT IN SQL clause instead intersect. 您可以使用NOT IN SQL子句代替。
@Query("SELECT a FROM A a WHERE a.userGroups not in (:userGroups)")
List<A> getAWithNoIntersectionInGroups(@param("userGroups") Set<UserGroup> userGroups)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.