简体   繁体   English

GORM - 在Grails中查询一对多关联

[英]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 现在我想找到所有类别为4的类别的课程

I tried writing HQL query: 我尝试编写HQL查询:

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 ? 如何编写正确的HQL或正确的withCriteria查询?

You can use withCriteria query: 您可以使用withCriteria查询:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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