Grails gorm: Find missing entries in other table
문제
I have two tables: EventSession and EventTrack
EventSession hasMany EventTrack, but can also have no EventTrack. EventTrack belongsTo EventSession.
How do I get all EventSession that has no EventTrack (zero). Something like:
def es = EventSession.createCriteria()
def _es = es.list {
count("eventTracks") == 0 // I know this is wrong
}
Thanks.
다른 팁
HQL can do this for you:
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)
With createCriteria()
I think you can do the same with an left outer join, filtering where the id of EventTrack is null (not tested)
def es = EventSession.createCriteria().list() {
eventTracks {
isNull('id')
}
}
What about EventSession.findAllByEventTracksIsNull() ?
제휴하지 않습니다 StackOverflow