Currently I have several ways I've done this...
The first 2 ways are with stream filtering directly on entities:
List<CourseOffering> coursesTaught = professor
.getCourses()
.stream()
.filter(courseOffering -> schedule.equals(courseOffering.getSchedule()))
.collect(Collectors.toList());
List<CourseOffering> coursesTaught = courseOfferingRepository
.findAllBySchedule(schedule)
.stream()
.filter(courseOffering -> courseOffering.getProfessors().contains(professor))
.collect(Collectors.toList());```
And the next 2 are with repository methods, one of which was autogenerated.
```java
@Query(nativeQuery = true, value = """
SELECT co.*
FROM COURSE_OFFERING co
INNER JOIN COURSE_OFFERING_PROFESSOR cop
ON co.id = cop.course_offering_id
WHERE co.schedule_id = :scheduleId
AND cop.professor_id = :professorId""")
List<CourseOffering> findAllByScheduleAndProfessor(
@Param("scheduleId") Long scheduleId,
@Param("professorId") Long professorId
);```
with ```java
List<CourseOffering> coursesTaught = courseOfferingRepository.findAllByScheduleAndProfessor(scheduleId, professorId);```
And then the spring data created ```java
List<CourseOffering> findAllByScheduleAndProfessors(Schedule schedule, Professor professor);```
called with ```java
List<CourseOffering> coursesTaught = courseOfferingRepository.findAllByScheduleAndProfessors(schedule, professor);```
Im surprised this last one works (or appears to work) since CourseOffering : Professor is Many:Many, and I only passed in 1. (If I add a second professor it still finds the courses).
I definitely feel like a repository level method is best to use, but I dont like how the autogenerated one isn't very clear that it (seems to) work with only querying a single professor on a dual professor course