如何在grails命名查询中编写“不存在”查询?

时间:2013-08-15 13:57:08

标签: hibernate grails

如何在hibernate条件或命名查询中编写“不存在”查询?我试图获得一个命名查询not exists query,它返回与此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))

2 个答案:

答案 0 :(得分:2)

使用sqlRestriction。这将直接在最终查询中注入,因此您需要使用数据库列名。

School.createCriteria().list {
  sqlRestriction(" not exists(select 1 from student s where s.school_id = this_.id and ...)")
}

答案 1 :(得分:2)

在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))