Grails hibernate创建多个AND OR条件的条件

时间:2015-04-28 19:30:40

标签: hibernate gorm conditional-statements createcriteria

我能够使用grails / Hibernate创建3个条件查询创建Criteria如下

 def c = Domain.createCriteria()

    c.list(max: map.max ?: 10, offset: map.offset ?: 0) {

            and {
                "colStatus" in status
                "code" in codes
            }
            and {
                or {
                    ilike("pCode", pCode)
                    ilike("pDesc", pCode)
                }
            }

        order("DateCol3", "desc")
    }

现在我有额外的3个添加条件,内部OR条件,我需要添加到查询。我尝试过没有成功

 def c = Domain.createCriteria()

    c.list(max: map.max ?: 10, offset: map.offset ?: 0) {

            and {
                "colStatus" in status
                "code" in codes
            }
            and {
                or {
                    ilike("pCode", pCode)
                    ilike("pDesc", pCode)
                }
            }

            and {
                or {
                    ilike("eCode", eCode)
                    ilike("eDesc", eCode)
                }
            }

            and {
                or {
                    ilike("oCode", oCode)
                    ilike("oDesc", oCode)
                }
            }

        order("DateCol3", "desc")
    }

请建议并帮助我如何添加多个AND / OR条件。

1 个答案:

答案 0 :(得分:0)

我能够通过引入括号来解决这个问题

def c = Domain.createCriteria()

c.list(max: map.max ?: 10, offset: map.offset ?: 0) {

        {
            "colStatus" in status
            "code" in codes
        }
        ( {
            or {
                ilike("pCode", pCode)
                ilike("pDesc", pCode)
            }
        }
      )
      (
       {
            or {
                ilike("eCode", eCode)
                ilike("eDesc", eCode)
            }
        }
      )
      (  and {
            or {
                ilike("oCode", oCode)
                ilike("oDesc", oCode)
            }
        }
      )
    order("DateCol3", "desc")
}