2012-02-15 70 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])