在嵌套的List对象Hibernate,Jpa中通过多个值查找对象

时间:2018-04-20 19:39:38

标签: java spring hibernate jpa

我有学生实体。学生有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)

这是正确的方法吗?如果没有,是否有可能这样做?谢谢!

1 个答案:

答案 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)