oracle – grails条件查询返回空结果

前端之家收集整理的这篇文章主要介绍了oracle – grails条件查询返回空结果前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个域结构,如下所示

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

class Child {
 int gender
 string height
}

现在,我想得到所有父母的名单,他们有一个男孩(性别= 1),身高低于’180’cm,女孩(性别= 2)身高超过’150’cm.

我尝试了下面给出的标准

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

但它返回一个空列表,尽管有有效数据.

解决方法

将’和’更改为’或’后’孩子’.因为,逻辑’或’试图找到两个查询之间的并集,它始终为null.

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

您可以查看我做过的github项目,以便说明这个答案.

猜你在找的Oracle相关文章