grailsgrails-ormnamed-querydomain-object

Grails named queries not working with "in" statement


I have a Grails application (2.2.4). Where in domain class I have looks like this

class Author implements Serializable {
    ....
  static hasMany = [
    book : Book 
  ]
  static namedQueries = {
      hasGenre {genreNameList -> 
            book{
               genres {  
                   'title' in genreNameList 
           } 
       }
   }
  }
}

class Book implements Serializable{

    Author author
    Genres genres
    static belongsTo = [author: Author , genre: Genres ]
       static mapping = {
       .....
        author lazy: false
       }
}

class Genres implements Serializable{

    String title

 }

If I run the query as below, all the values are retrieved, and not only the Authors with atleast one book with genere in the genereNameList

String comaSeperatedGenereName = "genere1,genere2"
def genereNameList = comaSeperatedGenereName.split(",")
Author.hasGenre(genereNameList)

But If I change the namedQuery like the following,

      hasGenre {genreName -> 
            book{
               genres {  
                   eq 'title' , genreName 
           } 
       }

And If I pass a String like following

Author.hasGenre('genere1')

This works as expected. Is there something i'm missing?

Thanks in advance


Solution

  • There is a groovy in operator and I suspect that instead of the criteria in you are getting the groovy in operator.

    Try changing your code to

      static namedQueries = {
          hasGenre {genreNameList -> 
                book{
                   genres {  
                  'in'  'title', genreNameList 
               } 
           }
       }
      }