Grails gorm:在其他表中查找缺少的条目

时间:2012-08-27 23:26:23

标签: grails gorm

我有两个表:EventSession和EventTrack

EventSession有很多EventTrack,但也没有EventTrack。 EventTrack属于EventSession。

如何获取没有EventTrack(零)的所有EventSession。类似的东西:

def es = EventSession.createCriteria()
def _es = es.list {
   count("eventTracks") == 0  // I know this is wrong
}

感谢。

3 个答案:

答案 0 :(得分:1)

HQL可以为您完成此任务:

String hql = "select e from EventSession e " +
             " where not exists(select 1 from EventTrack t where t.eventSession = e)"
List<EventSession> eventsWithoutTracks = EventSession.executeQuery(hql)

使用createCriteria()我认为您可以使用左外连接执行相同操作,过滤EventTrack的id为null(未测试)

def es = EventSession.createCriteria().list() {
  eventTracks {
    isNull('id')
  }
}

答案 1 :(得分:1)

Where

EventSession.where { count(eventTracks) == 0 }

Criteria

EventSession.withCriteria {
  isEmpty 'eventTracks'
}

答案 2 :(得分:0)

EventSession.findAllByEventTracksIsNull()怎么样?

相关问题