Reputation: 267
I have Student entity. Students have list of subjects in ManyToMany relationship. Something like this:
@Entity
@Table(name = "students")
public class Student {
@ManyToMany(cascade = CascadeType.DETACH)
@JoinTable(name = "student_subject", joinColumns = @JoinColumn(name = "student_id"), inverseJoinColumns = @JoinColumn(name = "subject_id"))
private List<Subject> subjects ;
}
I want to get all students that contain any of the requested subjects in my repository, something like this:
List<Student> findBySubjects(List<Subject> requestedSubjects)
Is this the right way to go? If not, is it even possible to do it like this? Thanks!
Upvotes: 0
Views: 445
Reputation: 767
You can use @Query
annotation.
@Query("SELECT student FROM Student student INNER JOIN student.subjects subject WHERE subject IN (:subjects)")
List<Student> findBySubjects(@Param("subjects") List<Subject> requestedSubjects)
Upvotes: 1