2012-02-15 66 views
0

关联我有一个的很多副作用很多关系的课程和分类的Grails GORM查询使用HQL

class Course { 

    String code  

    static hasMany = [categories:CourseCategory] 
} 


Class CourseCategory { 

    String name 
} 

我需要根据类别的名单上查询课程之间。我已经试过查询

courseInstanceList = Course.findAll("from Course c inner join c.categories cts where cts.id in :categoryIds",[categoryIds:categoryIds]) 

但是,这个查询返回课程和CourseCategories - 只是想知道如何创建一个查询,只是返回课程?

回答

0

可以使用createCriteria方法:

def c = Course.createCriteria() 
println (c.listDistinct { 
    categories { 
     'in' 'id', [1L, 2L, 3L] 
    } 
}) 
+1

该查询返回c我们有任何指定的类别ID。是否有可能只返回具有指定的所有类别ID的课程? – nonie 2012-02-22 00:06:55

0

近三年过去......)

反正这里的答案:

courseInstanceList = Course.findAll("SELECT distinct c from Course c inner join c.categories cts where cts.id in :categoryIds",[categoryIds:categoryIds]) 

得到公正类别:

courseInstanceList = Course.findAll("SELECT cts from Course c inner join c.categories cts where cts.id in :categoryIds",[categoryIds:categoryIds])