Querying Domain class with hasMany String Type

25 / Sep / 2012 by Puneet Behl 2 comments

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

FOUND THIS USEFUL? SHARE IT

comments (2)

Leave a Reply

Your email address will not be published. Required fields are marked *