У меня есть 3 группы лиц со студентом (1 - ∞) и рейтинги студентов (1 - ∞)
Группа
@Entity
@Table(name = "`group`")
public class Group implements Serializable {
@OneToMany(fetch = FetchType.EAGER)
@JoinColumn(name = "id_group")
private List<Student> students;
}
Студент
@Entity
@Table(name = "student")
public class Student implements Serializable {
@OneToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
@JoinColumn(name = "id_student")
public List<Rating> ratings;
@ManyToOne
@JoinColumn(name = "id_group")
private Group group;
}
Рейтинг
@Entity
@Table(name = "rating")
public class Rating implements Serializable {
@Temporal(TemporalType.DATE)
@Column
private Date date;
@ManyToOne
@JoinColumn(name = "id_student")
public Student student;
}
Моя цель состоит в том, чтобы выбрать все группы по идентификаторам со студентами с оценками учащихся между 2 датами.Решения, которые я пробовал
@Query(value = "select g from Group g INNER JOIN g.students s ON g.id = :id INNER JOIN s.ratings r ON r.student.id = s.id and r.date between :dateStart and :dateEnd GROUP BY g.id")
@Query(value = "SELECT * FROM Group g INNER JOIN student ON g.id_group = :id RIGHT JOIN rating on student.id_student = rating.id_student and rating.`date` between :dateStart and :dateEnd GROUP BY g.id_group",nativeQuery = true)
@Query(value = "select g from Group g WHERE g.id = :id and g.students IN (select s from g.students s JOIN s.ratings r where r.date between :dateStart and :dateEnd) GROUP BY g.id")