Querying Domain class with hasMany String Type
In my recent project there was a use case where I had to query domain’s hasMany but relationship was not with any domain class but rather String type.
Consider example
[java]
class Blog {
static hasMany = [tags: String]
}
[/java]
Now to find all the blogs with tag “grails” , our normal way of querying hasMany relationships will not work in this scenario. In this scenario hql can be usefull:
[java]
Blog.findAll("from Blog b where :tag in elements(b.tags)", [tag: "grails"])
[/java]
This will return you all the blogs with “grails” tag.
Hope this helps.
Puneet
puneet@intelligrape.com
Good to know this different and useful scenario. Thanks for sharing.
I need this. Thanks.