hibernategrails-ormconditional-statementscreatecriteria

Grails hibernate create criteria multiple AND OR Conditions


I was able to do 3 conditions query using grails/Hibernate create Criteria as follows

 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")
    }

Now i have additional 3 add conditions with internal OR conditions which i need add upto the query. I tried following with no success

 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")
    }

Please suggest and help me how i should be adding up multiple AND/OR conditions.


Solution

  • I was able to resolve this with introduction of parenthesis

    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")
    }