0

I have these annotations for the models:

@Many2Many(other = Course.class, join = "registrations", sourceFKName = "student_uid", targetFKName = "course_uid")
public class Student extends Model {
}

@Many2Many(other = Student.class, join = "registrations", sourceFKName = "course_uid", targetFKName = "student_uid")
public class Course extends Model {
}

How do I get all Students belong to a course UID?

ipolevoy
  • 5,432
  • 2
  • 31
  • 46
ln9187
  • 730
  • 1
  • 7
  • 23

1 Answers1

2

First, you do not need to specify the same annotation twice. This will work just the same:

public class Student extends Model {}

@Many2Many(other = Student.class, join = "registrations", sourceFKName = "course_uid", targetFKName = "student_uid")
public class Course extends Model { }

Second, your case is described on this page: http://javalite.io/many_to_many_associations#select-related-objects

so, you would:

Course course = Course.findById(id);
List<Student> students = course.getAll(Student.class);

That is all!

ipolevoy
  • 5,432
  • 2
  • 31
  • 46