2

我有一个培训领域课程

    class Training {

     String type
     Date createdOn
     Date modifiedOn

    static belongsTo = [course: Course]
    static hasMany = [attachments: Attachment]
  }

我有课程领域课程

     class Course {

          String name

          static hasMany = [trainings: Training, tracks: Track]
          static belongsTo = Track
   }

和跟踪域类

    class Track {
        String name 
    }

现在我有一个过滤器(这是 gsp 页面,它将 id 作为参数发送到控件),它根据课程和跟踪选择培训

现在说 params.courseId = 1 和 3

所以我写了查询

   def query = "FROM Training AS t WHERE  t.course.id IN (1,3)"
   trainingList = Training.findAll(query)

这是正确的 ii 得到所需的输出。

现在,当我说我有跟踪 ID 时,params.trackId = 1,2

    def query = "FROM Training AS t WHERE  t.course.tracks.id IN (1,2)"
    trainingList = Training.findAll(query) 

这不起作用..如何在上述关联中编写正确的查询。

4

2 回答 2

4

不同之处在于您正在查询一对多关联,即“多”方面。

假设 HQL:

def query = "FROM Training AS t WHERE exists " +
  "(from t.course.tracks AS tr where tr.id IN (1,2))"

或标准:

def trainings = Training.withCriteria {
   course {
     tracks {
       in('id', [1, 2])
     }
  }
}
于 2012-09-13T14:54:34.137 回答
0

我没有进行更深入的研究,但是这个查询给出的结果与您首先编写的查询相同

def trainingList = Training.findAll ("from Training as trnin "+
"join trnin.course.id as trinincr"+
"join trinincr.tracks.id"+
" where trnin.course.id in (1,3)")
于 2013-11-19T10:28:16.900 回答