简体   繁体   中英

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 ?

You can use withCriteria query:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM