Consider this simple database schema:
User Course StudentCourse Student
+-------------+ +-------------+ +-------------+ +-------------+
| -user_id |1 *| -course_id |1 *| -course_id |* 1| -student_id |
| |---->| -user_id |---->| -student_id |---->| |
+-------------+ +-------------+ +-------------+ +-------------+
[1 *] = 1 to many relationship
I created objects for User, Course, and Student objects and set the following mappings:
User -> Course - one to many
Course -> Student - many to many
In my Java classes, I can access the user's courses by calling user.getCourses(), and I can access all the students in the course by calling course.getStudents(). I want to find all the students in all courses taught by a particular user , for example user.getCourse().getStudents(), but since user.getCourses()returns Collection<Course>, I cannot call course.getStudents()to the Collection. How do I implement this with Hibernate? Is a named query my only option?