GORM - a request from one of the many associations in Grails

I have a course domain

class Course { String name static hasMany = [categories: Category] } 

Category Domain Class

 class Category { String name } 

therefore, a course here can have several categories.

Now I want to find all courses in which there is a category whose id 4

I tried to write an HQL query:

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

which gives an error.

How to write the correct HQL or the correct criteria query?

+4
source share
1 answer

You can use the criteria query:

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

All Articles