如何在休眠条件或命名查询中编写“不存在”查询?我正在尝试获取一个命名查询不存在查询,它返回与此 Oracle SQL 查询相同的结果:
select *
from SCHOOL a
where not exists (select 1
from STUDENT b
where B.SCHOOL_ID=a.id
and B.STATUS_ID not in (0,1,2,3,4))
使用sqlRestriction
. 这将直接在最终查询中注入,因此您需要使用数据库列名。
School.createCriteria().list {
sqlRestriction(" not exists(select 1 from student s where s.school_id = this_.id and ...)")
}
在 HQL 中:
select s from School s where not exists (
select st.id from Student st
where st.school = s
and st.statusId not in (0,1,2,3,4))