Hussy
Hussy

Reputation: 2029

GORM - Query one to many association in Grails

I have Course Domain

class Course {
    String name

    static hasMany = [categories: Category]
}

Category domain class

class Category {
    String name 
}

so here a Course can have multiple Category.

Now I want to find all the courses which has a Category whose id is say 4

I tried writing HQL query:

def courseList = Course.findAll("from Course as c where c.categories.id in (4)")

which gives an error.

How to write correct HQL or a proper withCriteria query ?

Upvotes: 4

Views: 3555

Answers (1)

Anton N
Anton N

Reputation: 2375

You can use withCriteria query:

Course.withCriteria { 
    categories { 
        eq 'id', new Long(4)
    }
}

Upvotes: 4

Related Questions