Hibernate 一对多关联的Grails Hql查询

Hibernate 一对多关联的Grails Hql查询,hibernate,grails,groovy,hql,gorm,Hibernate,Grails,Groovy,Hql,Gorm,我有一个培训领域的课程 class Training { String type Date createdOn Date modifiedOn static belongsTo = [course: Course] static hasMany = [attachments: Attachment] } class Course { String name static hasM

我有一个培训领域的课程

    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 
    }
我有课程领域课程

    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 
    }
并跟踪域类

    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)
这是正确的,我得到了想要的输出

现在,当我说我有轨迹id时,params.trackId=1,2

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

这是行不通的。。如何在上面提到的关联中编写正确的查询。

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

假设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])
     }
  }
}

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

假设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])
     }
  }
}

我没有深入研究过,但是这个查询给出了与您第一次编写的查询相同的结果

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)")

我没有深入研究过,但是这个查询给出了与您第一次编写的查询相同的结果

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)")

给出错误:Class:org.hibernate.hql.ast.QuerySyntaxException消息:意外标记:tr靠近第1行第75列[来自com.mycompany.opal.Training AS t WHERE exists从t.course.tracks中选择track tr,其中tr.id IN 1,2]已更新。虽然我没有试过,但应该可以。不过,我对标准非常确定。当没有belongsTo时,是否可以提取列表。给出错误:Class:org.hibernate.hql.ast.QuerySyntaxException消息:意外标记:tr靠近第1行第75列[FROM com.mycompany.opal.Training AS t WHERE exists从t.course.tracks选择track tr FROM t.course.tracks WHERE tr.id in 1,2]更新。虽然我没有试过,但应该可以。不过,我对标准很确定。当没有belongsTo时,可以提取列表。为什么不使用select t from Course c join c.trainings t,其中c.id在1,2中。检查此处的样本为什么不使用课程c中的选择t加入c.t,其中c.id在1,2中。在这里检查样品