我有3个相互绑定的实体。
组
@Entity
@Table(name = "`group`")
public class Group implements Serializable {
@OneToMany(mappedBy = "group",fetch = FetchType.EAGER)
@Fetch(FetchMode.SUBSELECT)
private List<Student> students;
}
学生
@Entity
@Table(name = "student")
public class Student implements Serializable {
@OneToMany(mappedBy = "student",cascade = CascadeType.ALL,fetch = FetchType.EAGER)
@Fetch(FetchMode.SUBSELECT)
private List<Rating> ratings;
}
评分
@Entity
@Table(name = "rating")
public class Rating implements Serializable {
@Temporal(TemporalType.DATE)
@Column
private Date date;
@ManyToOne
@JoinColumn(name = "id_student")
private Student student;
}
我需要按ID与所有学生之间的日期进行分组。 我尝试这些情况来解决问题
@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")
@Query(value = "SELECT g from Group g JOIN g.students s ON g.id = :id JOIN s.ratings r on s.id = r.student.id and r.date between :startMonth and :endMonth")
但是这些都不是必须过滤的等级。
在此查询中,所有本地工作
SELECT * from `group` JOIN student ON `group`.id_group = 1 LEFT JOIN rating on student.id_student = rating.id_student and rating.`date` between '2019-02-01' and '2019-02-28';
任何人都知道发生了什么问题或如何将本地查询转换为JPQL?
UPD
为避免有关日期的问题,我对日期使用相同的逻辑以获取日期之间的所有评分。
存储库
@Query(value = "Select r from Rating r where r.student.id = :studentId and r.stageOfApprove = :stageOfApprove and r.date between :startMonth and :endMonth order by r.paragraph.id asc ")
List<Rating> findAllRatingsByIdStudentAndStageOfApproveGreaterThan(@Param("studentId") Long studentId, @Param("stageOfApprove") Integer stageOfApprove,
@Param("startMonth") Date startMonth, @Param("endMonth") Date endMonth);
服务
DateTime currentDate = new DateTime();
DateTime minDays = currentDate.dayOfMonth().withMinimumValue().withTimeAtStartOfDay();
DateTime maxDays = currentDate.dayOfMonth().withMaximumValue().withTimeAtStartOfDay();
ratings = ratingRepository.findAllRatingsByIdStudentAndStageOfApproveGreaterThan(studentId,1,minDays.toDate(),maxDays.toDate());
return ratings;