复杂的 JPQL 查询返回 0 零结果在查询中获取 Table 两次
Complex JPQL Query Returning 0 Zero Results Fetching Table Twice in Query
我正在开发一个应用程序,公交车司机可以在他们处理的路线上与 children 的 parents 群聊。
我想要一个选项,可以在公交车到达最后一站之前向所有聊天发送消息。
我试过这个查询,但总是 returns 0 个结果。什么时候它应该至少返回一个。
1 | @Query("select g from GroupChat g " +
2 | "join g.route r " +
3 | "join r.bus b " +
4 | "join b.mostRecentStop mrs " +
5 | "join r.stops s " +
6 | "join s.children c " +
7 | "where r.routeId = :routeId " +
8 | "and s.stopTime <= mrs.stopTime " +
9 | "and g.childId = c.childId ")
10|Set<GroupChat> getAllGroupChatsOnRouteForPreviousStops(@Param("routeId") short routeId);
GroupChats PK 是 routeId-childId
第 2 行获取与群聊关联的路由。
3 号线获取该路线的公共汽车。
4 号线获取公交车到达的最近站点。
第 5 行获取路线上的所有站点
第 6 行获取路线上的所有 children(即来自每个站点的每个 child)
第 7 行仅过滤查询的路由。
第 8 行筛选最近停止之前的停止
第 9 行仅筛选 children 在其余站点上的群聊。
我做错了什么?
由于涉及JPQL中的复杂查询,建议使用Native Query。
@Query(value="select g from GroupChat g
join g.route r
join r.bus b
join b.mostRecentStop
join r.stops s
join s.children c
where r.routeId = :routeId
and s.stopTime <= mrs.stopTime
and g.childId = c.childId",
nativeQuery=true
)
Set<GroupChat> getAllGroupChatsOnRouteForPreviousStops(@Param("routeId") short routeId);
我正在开发一个应用程序,公交车司机可以在他们处理的路线上与 children 的 parents 群聊。
我想要一个选项,可以在公交车到达最后一站之前向所有聊天发送消息。
我试过这个查询,但总是 returns 0 个结果。什么时候它应该至少返回一个。
1 | @Query("select g from GroupChat g " +
2 | "join g.route r " +
3 | "join r.bus b " +
4 | "join b.mostRecentStop mrs " +
5 | "join r.stops s " +
6 | "join s.children c " +
7 | "where r.routeId = :routeId " +
8 | "and s.stopTime <= mrs.stopTime " +
9 | "and g.childId = c.childId ")
10|Set<GroupChat> getAllGroupChatsOnRouteForPreviousStops(@Param("routeId") short routeId);
GroupChats PK 是 routeId-childId
第 2 行获取与群聊关联的路由。
3 号线获取该路线的公共汽车。
4 号线获取公交车到达的最近站点。
第 5 行获取路线上的所有站点
第 6 行获取路线上的所有 children(即来自每个站点的每个 child)
第 7 行仅过滤查询的路由。
第 8 行筛选最近停止之前的停止
第 9 行仅筛选 children 在其余站点上的群聊。
我做错了什么?
由于涉及JPQL中的复杂查询,建议使用Native Query。
@Query(value="select g from GroupChat g
join g.route r
join r.bus b
join b.mostRecentStop
join r.stops s
join s.children c
where r.routeId = :routeId
and s.stopTime <= mrs.stopTime
and g.childId = c.childId",
nativeQuery=true
)
Set<GroupChat> getAllGroupChatsOnRouteForPreviousStops(@Param("routeId") short routeId);