Grails:如何在多对多的映射中查询对象? [英] Grails: How to query objects in many to many mapping?

查看:101
本文介绍了Grails:如何在多对多的映射中查询对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  class Student {
int age
static hasMany = [课程:课程]
}

课程{
字符串名称
static hasMany = [学生:学生]
}

我想找到7岁的学生参加课程(ID为1)。



我可以使用动态查找器或条件构建器或HQL吗?



我不想做下面的事情,因为它会加载所有学生如此低效:

  def course = Course.get(1); 
course.students.findAll {it.age == 7}


解决方案

  def studs = Student.withCriteria {
eq('age',7)
courses {
eq('id', 1)
}
}

它位于 GORM doc ,查询关联部分。


Hello I have the follwing domain classes.

class Student {
   int age
   static hasMany = [courses:Course]
}

class Course {
   String name
   static hasMany = [students:Student]
}

I want to find the Students taking Course (with id 1), with age 7.

Could I do that with dynamic finder or criteria builder or HQL?

I do not want to do following as it load all students so inefficient:

def course = Course.get(1);
course.students.findAll{ it.age == 7 }

解决方案

def studs = Student.withCriteria {
  eq('age', 7)
  courses {
    eq('id', 1)
  }
}

It's in GORM doc, section "Querying Associations".

这篇关于Grails:如何在多对多的映射中查询对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆