How to get PagedResultList using chained named queries and adding additional criteria closure?

StackOverflow https://stackoverflow.com/questions/13268084

  •  27-11-2021
  •  | 
  •  

Question

i am trying to get a pagedResultList using chained named queries, but i seem to fail. Any hints or tips on how this can be achieved?

See modified example below from the Grails documentation that should illustrate my needs

def books = Publication.recentPublications.grailsInTitle.list(params) {
    or {
        like 'author', 'Tony%'
        like 'author', 'Phil%'
    }
}

This always returns an ArrayList..

When or remove the additional criteria and use it like below it works

def books = Publication.recentPublications.grailsInTitle.list(params)

I would like to add some criteria closures, any hints or tips on how i could achieve this?

Était-ce utile?

La solution

I am facing same problems with named queries. This is my solution applied to your classes. Comment if it works for you.

class Publication {
    //fields, constraints, etc.
    namedQueries = {
        authoredLike { String authorName ->
        if (authorName) {
            like 'author', authorName
        }
        // untested, but you get the point, please experiment
        authoredLikeMany { List<String> authors ->
            authors.each { String authorName -> like 'author', authorName }
        }
    }
}

def tonyBooks = Publication.recentPublications.grailsInTitle.authoredLike('Tony%').list(params)
def tonyAndPhilBooks = Publication.recentPublications.grailsInTitle.authoredLikeMany(['Tony%', 'Phil%']).list(params)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top