2013-04-02 84 views
4

我有场域GORM - 查询一对多关联Grails中

class Course { 
    String name 

    static hasMany = [categories: Category] 
} 

类别域类

class Category { 
    String name 
} 

所以这里课程可以有多个类别。

现在我想找到所有具有类别id为例如4

我试着写HQL查询课程:

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

它给出了一个错误。

如何编写正确的HQL或正确的withCriteria查询?

+0

那是一个笔误或者是你真的想使用的hasMany和属于关联的同一侧?只是看起来不正确。如果一门课程有许多类别,则它不能属于一个类别。 – uchamp

+0

@ uchamp:这是类型错误,更新了我的问题.. – Hussy

回答

4

您可以使用withCriteria查询:

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