Quantcast
Channel: Spring Community Forums - Data
Viewing all articles
Browse latest Browse all 297

filter a collection of child elements in a OneToMany relationship

$
0
0
I'm a little new to Spring Data and I'm having trouble finding anything about this in the docs. All I need to do is put a filter on a collection of child elements.

I have two tables - courses and exercises - with a one-to-many relationship between courses and exercises. The exercise table has an "active" filed and I want to filter the list of exercises I get back so that I only see "active" exercises. The SQL would look like this:

Code:

select c.*, e.*
from courses c, exercises e
where e.course_id = c.course_id
and e.active = true

Here is the relevant portion of my Course class
Code:

@OneToMany(fetch = FetchType.LAZY)
        @JoinColumn(name = "course_id")
    private List<Exercise> exercises;

I'm generally searching for courses by ID, so I set up the following method in my Course repository

Code:

        @Query("select c from Course c join c.exercises e where c.id = :courseID and e.active = true")
        public Course findOneWithActiveExercises(@Param("courseID") Long courseID);

This runs with no errors, but the list of attached Exercises contains entries where active = false. QueryDSL seems like it could provide the answer, but I've looked into the docs and I don't see a clear way to manipulate collections of child elements. Any advice would be appreciated.

Thanks,
Alex

Viewing all articles
Browse latest Browse all 297

Trending Articles