简体   繁体   English

Grails 3:查找以多对多关系添加到另一个域的域(未为参数 1 指定值

[英]Grails 3: find domains that were added to another one in many-to-many relationship (No value specified for parameter 1

I'm a Grails newbie and I found the following obstacle:我是 Grails 新手,我发现了以下障碍:

I have 2 domains: Course and Student, they have a many-to-many relationship (a Course can have several students, a student can enroll in several courses) and the student belongs to the course.我有 2 个域:Course 和 Student,它们是多对多的关系(一个 Course 可以有几个学生,一个学生可以注册几门课程)并且学生属于该课程。

So, when I add a student to a course, I want to be able to find what Courses have added a specific student.因此,当我将学生添加到课程时,我希望能够找到哪些课程添加了特定学生。

I tried to use:我尝试使用:

def s = Student.get(id) def s = Student.get(id)

def c = Course.findAllByStudents(s) def c = Course.findAllByStudents(s)

But grails keeps telling me "No value specified for parameter 1".但是 grails 一直告诉我“没有为参数 1 指定值”。

Can you guys throw some light into this?你们能不能对此有所了解?

Course.findAllByStudents expects as parameter Set of Students but you are supplying it with single instance of Student, that's why you are getting "No value specified for parameter 1" . Course.findAllByStudents期望作为参数Set of Students 但您提供的是 Student 的单个instance ,这就是为什么您会收到"No value specified for parameter 1"

To find in what Courses is Student.找出哪些课程是学生。 If you created domain classes like this:如果您创建了这样的域类:

class Course {
    //some Course attributes
    static hasMany = [students: Student] 
}

class Student {
     //some Student attributes
     static hasMany = [courses: Course]
     static belongsTo = Course
} 

then you can simply use s.courses .那么你可以简单地使用s.courses

If you are not two-way mapping that relationship.如果你不是双向映射那关系。 You can create criteria like this:您可以创建这样的标准:

Course.withCriteria {
    createAlias 'students', 's'
    eq 's.elements', s
}    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM