The grails criteria query returns an empty result

I have a domain structure as follows

class Parent {
  static hasMany = [childs:Child]
}

class Child {
 int gender
 string height
}

Now, I want to get a list of all parents who have a boy (gender = 1) with a size of at least 180 cm tall and a girl (gender = 2) with more than "Height 150 cm.

I tried the criteria below

def criteria = Parent.createCriteria()
def parents = criteria.list() {
        childs {
            and {
                and {
                    eq("gender", 2)
                    ge("height", 150)
                }
                and {
                    eq("gender", 1)
                    le("height", 180)
                }
            }
        }
    }
}

but it returns an empty list, although there is reliable data.

+4
source share
1 answer

Change 'and'to 'or'after "childs". Since boolean 'or'tries to find union between two requests that would always be empty.

List<Parent> parents = Parent.createCriteria().listDistinct {
      and {
          childs {
              or {
                  and {
                      eq("gender", 2)
                      ge("height", 150)
                  }
                  and {
                      eq("gender", 1)
                      le("height", 180)
                  }
              }
          }
      }
  }

github, .

+1

Source: https://habr.com/ru/post/1569406/


All Articles