当前位置:首页>开发>正文

求hibernate多对多查找相同SET的HQL

2023-07-04 11:57:15 互联网 未知 开发

 求hibernate多对多查找相同SET的HQL

求hibernate多对多查找相同SET的HQL

其实第二个相对容易些。
选取的逻辑是,目标student中不存在任何一个teacher不在指定的student的teachers中存在,即被包含关系。
所以HQL可以这么写(已验证):
select s1
from Student s1, Student s2
where s2.id=4
and not exists
(from s1.teachers t where t not in elements(s2.teachers))

而第一个问题则更复杂些,除了判断s1.teachers被包含于s2.teachers外,还得判断s2.teachers被包含于s1.teachers
select s1
from Student s1, Student s2
where s2.id=4
and not exists
(from s1.teachers t1 where t1 not in elements(s2.teachers))
and not exists
(from s2.teachers t2 where t2 not in elements(s1.teachers))

补充:
如果不想查出没有老师的学生,那么需要判断size(s1.teachers)>0