2

I have a few domain classes similar to the following:

class Position {
    String code
    String title

    static hasMany = [relations: Relation]
}

class Unit {
    String code
    String title

    static hasMany = [relations: Relation]
}

class Relation {
    Position position
    Unit unit

    static belongsTo = [
        position: Position,
        unit: Unit
    ]
}

I am attempting to use criteria to find all positions that do not have any relations. I know this can be solved using HQL but I find the criteria to be cleaner when building dynamic criteria versus building a dynamic HQL string.

Is there a way to use criteria to do something like:

Position.withCriteria { isNull('relations') }

I have tried the above but I always get a list containing 0 elements even though I know there are unrelated positions in the table.

thewidgetsmith
  • 194
  • 1
  • 8

1 Answers1

2

For collections you need to use isEmpty() instead of isNull().

Position.withCriteria { isEmpty('relations') }
Joshua Moore
  • 24,706
  • 6
  • 50
  • 73