简体   繁体   中英

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:

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.

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 c = Course.findAllByStudents(s)

But grails keeps telling me "No value specified for parameter 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" .

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 .

If you are not two-way mapping that relationship. You can create criteria like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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