[英]Grails GORM Querying the many side of an association using 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])
但是此查询返回课程和课程类别-仅想知道如何创建仅返回课程的查询?
快三年了...)
无论如何,这是答案:
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])
您可以使用createCriteria方法:
def c = Course.createCriteria()
println (c.listDistinct {
categories {
'in' 'id', [1L, 2L, 3L]
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.