加入收藏 | 设为首页 | 会员中心 | 我要投稿 济源站长网 (https://www.0391zz.cn/)- 数据工具、数据仓库、行业智能、CDN、运营!
当前位置: 首页 > 站长百科 > 正文

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

发布时间:2021-01-12 03:21:15 所属栏目:站长百科 来源:网络整理
导读:我有一个域结构,如下所示 class Parent { static hasMany = [childs:Child]}class Child { int gender string height} 现在,我想得到所有父母的名单,他们有一个男孩(性别= 1),身高低于’180’cm,女孩(性别= 2)身高超过’150’cm. 我尝试了下面给出的标准 def

我有一个域结构,如下所示

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项目,以便说明这个答案.

(编辑:济源站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读