我有学生实体。学生有ManyToMany关系的主题列表。像这样:
@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 ;
}
我想让所有包含我的存储库中任何所请求主题的学生,如下所示:
List<Student> findBySubjects(List<Subject> requestedSubjects)
这是正确的方法吗?如果没有,是否有可能这样做?谢谢!
答案 0 :(得分:1)
您可以使用@Query
注释。
@Query("SELECT student FROM Student student INNER JOIN student.subjects subject WHERE subject IN (:subjects)")
List<Student> findBySubjects(@Param("subjects") List<Subject> requestedSubjects)