Quantcast

Help needed to query association multiple times

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Help needed to query association multiple times

mikemike
This post has NOT been accepted by the mailing list yet.
Example:

A book can have many tags for example romance, fiction, children, adult
A book like Harry Potter has tags including fiction, fantasy, children, etc

Class is defined such
class book {
  static hasMany = [tags Tag]
}

Now my question is how to query all books that have both fantasy and children tags?

        def result1 = Book.createCriteria().list{
                tags{
                    eq("tagValue", "fantasy" )
                };
        }

works fine to retrieve fantasy books, but I just cannot make a criteria for "fantasy AND children".

        def result1 = Book.createCriteria().list{
         and {
                tags{
                    eq("tagValue", "fantasy" )
                }
                tags{
                    eq("tagValue", "children" )
                }
         }
        }

returns empty results

If querying by criteria is not possible for such structure, please let me know if there are alternative efficient ways to query.

Thank you very much.



 





Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Help needed to query association multiple times

julianhall
This post has NOT been accepted by the mailing list yet.
Have you tried:


                tags{ 
                    and {
                        eq("tagValue", "fantasy" ) 
                        eq("tagValue", "children" )
                    } 
                } 
Loading...