문제

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.

도움이 되었습니까?

해결책

Where:

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

Criteria:

EventSession.withCriteria {
  isEmpty 'eventTracks'
}

다른 팁

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() ?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top