Wednesday, June 2, 2010

Querying simple collections in grails

Imagine a domain class has a collection of simple types like e.g.

class Domain
{
static hasMany[strings: String]
}

What would you do if you wanted to have a set of all instances of Domain that have a string called "blabla". Well my first guess was to use the dynamic method findBy, but this does not work. The same goes for using Domain.withCriteria{}. I found the solution in a jira where you can find a feature request about that. Meanwhile you have to do the querying yourself. I have included a static method within my Domain class to avoid DRYness:

static def match(String matchThis)
{
Domain.executeQuery('from DBUser where :name in elements(strings)', [name: matchThis])
}

Problem solved...

No comments:

Post a Comment