Take a look at
Criteria section - that has an example of querying associations.
Person.createCriteria().list
{
hobbies
{
eq(
'name', 'diving')
}
}
note the quote around the property name.
Regards,
Kamal
FrugalHorn wrote:
I am a newbie to Grails. So sorry if this is explained earlier. I have two domain classes. Person and Hobby.
class Person
{
String name
static hasMany = [hobbies:Hobby]
}
class Hobby
{
String name
static hasMany = [persons:Person]
static belongsTo = Person
}
So now they have a many to many(m:n) relationship. Then I added a few dummy records as persons and hobbies. So how should I write a criteria query to get persons with 'diving' hobby.
My best shot was:
Person.createCriteria().list
{
and
{
hobbies
{
eq(name, 'diving')
}
}
}
So any help regarding this will be thankful. Where can I find more documentation about the syntax part of writing criteria queries. The documentation in Grails book and site I feel is not adequate.
http://grails.org/doc/1.1.x/guide/5.%20Object%20Relational%20Mapping%20(GORM).html#5.4.2 Criteria