1

I want to get all rows that match some arbitrary criteria, then I want to also include any rows whose id is the parentId for any of those rows.

def criteria = MovieWorkbook.whereAny {
    and { 
        ilike("name", name)
        or {
            eq("status", "open")
            eq("status", "pending")
        }
    }
}

// this fails - returns nothing
criteria.or {
    inList("id", criteria.list()*.parentId )
}
criteria.list()
Scott Ingram
  • 85
  • 1
  • 11
  • Does it have to be a detached query? Have you looked at named queries and chaining them? http://grails.org/doc/latest/ref/Domain%20Classes/namedQueries.html – Joshua Moore Oct 05 '14 at 10:21
  • Yes, the query needs to be composed dynamically at invocation, something which I don't think is supported by a named query. In my example, name is supplied by a variable. In my actual problem, several fields are supplied by loops and variables. – Scott Ingram Oct 06 '14 at 08:02
  • I'm pretty sure that a named query can dynamically build it's criteria based upon the parameters. e.g. eq(somePropertyName, someValue) – Joshua Moore Oct 06 '14 at 08:08
  • Sure, but during static initialization. I can't find any documentation on what syntax would be required to create a namedQuery at runtime, nor pass in parameters as though the namedQuery were a method. – Scott Ingram Oct 06 '14 at 09:08
  • Not only does the documentation show examples of using parameters (http://grails.org/doc/latest/ref/Domain%20Classes/namedQueries.html) but there are some SO posts too: http://stackoverflow.com/questions/15947481/dynamically-creating-a-query-based-on-params-being-passed-to-a-controller You can do anything you would normally in a criteria in a named query. Including composing it dynamically based on the parameters. – Joshua Moore Oct 06 '14 at 09:11
  • Ah! I finally see the example where pageCount is a parameter. Thanks for pointing that out. Makes sense once I realize a namedQuery is "just" a closure. So, I'm back to essentially my original question: how to use a namedCriteria as its own criteria. I see how to chain them together in a logical AND. I don't see a logical OR, which is what I need -- "give me stuff, plus, that same stuff's parents." Unless you see something I'm overlooking (again), I still have to run the query once, then pass its own results back into it as additional criteria. – Scott Ingram Oct 07 '14 at 02:58

0 Answers0