简体   繁体   English

带有jparepository的复杂查询。 三个表联接

[英]Complicated query with jparepository. Three tables join

I want to filter entities with next way: get distinct 10 top disciplines, which are ordered by students count(attendance) for sessions between some period of time. 我想用另一种方式过滤实体:获得10个不同的顶级学科,这些学科按学生在某个时间段内的学习次数(出勤率)排序。

  • Discipline has List<Session> sessions; 学科List<Session> sessions; //bidirectional //双向
  • Every Session has its own List<Student> students; 节课都有自己的List<Student> students; //bidirectional //双向

So, Student didn't know anything about Discipline, only via Session. 因此,学生仅对Session并不了解纪律。 And I want to write using Spring JpaRepository something like: 我想使用Spring JpaRepository编写如下内容:

List<Discipline> findTop10DisciplinesDistinctOrderBySessionsStudentsCountAndBySessionsBetween(Date from, Date to);

but there are some restrictions. 但是有一些限制。 It didn't work for me 这对我没用

No property count found for type Student! 找不到学生类型的财产计数! Traversed path: Discipline.sessions.students. 遍历的路径:Discipline.sessions.students。

Query looks like this(mysql): 查询看起来像这样(mysql):

select d.*
from session as ses 
inner join    
(select st.*
from student as st 
inner join session as ses on ses.id = st.session_id
where ses.time > (curdate() - interval 30 day)
group by st.session_id
order by count(st.id) desc 
limit 10) as st2
on ses.id = st2.session_id
inner join discipline as d on ses.discipline_id = d.id

But how to add this query to jpa repository? 但是如何将此查询添加到jpa存储库?

Derived queries, those that get created from the method name are not suitable for use cases as yours. 派生查询(从方法名称创建的查询)不适合您的用例。 Even if it would work it would result in an unwieldy method name. 即使可行,也将导致笨拙的方法名称。 Use an @Query with JPQL or SQL query instead. 使用带有JPQL或SQL查询的@Query代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM