#Best Way to accomplish joining and filtering

1 messages · Page 1 of 1 (latest)

burnt niche
#

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
rapid pythonBOT
#

<@&987246883653156906> please have a look, thanks.

rapid pythonBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

meager ridge
#

its usually better to filter in the db because they are very efficient at it and ur also sending less data over the wire

burnt niche
#

I forgot that I can print the hibernate query from the autogenerated method. It just completed the full join into the professor table (my 'optimized' query stopped short at the join table since I already had the IDs from the entities.

Hibernate: 
    select
        courseoffe0_.id as id1_4_,
        courseoffe0_.arranged as arranged2_4_,
        courseoffe0_.course_id as course_i5_4_,
        courseoffe0_.delivery_id as delivery6_4_,
        courseoffe0_.reserved as reserved3_4_,
        courseoffe0_.schedule_id as schedule7_4_,
        courseoffe0_.section as section4_4_ 
    from
        course_offering courseoffe0_ 
    left outer join
        course_offering_professor professors1_ 
            on courseoffe0_.id=professors1_.course_offering_id 
    left outer join
        professor professor2_ 
            on professors1_.professor_id=professor2_.id 
    where
        courseoffe0_.schedule_id=? 
        and professor2_.id=?```
#

should I let it do that or use my sql 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

#

heck I'll just have both options

#

in case u have the ID and not the full entity

meager ridge
#

its way to late for me to think about sql now. maybe someone else can help.