微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Spring JPA组通过查询可分页注入不必要的ID导致语法错误

如何解决Spring JPA组通过查询可分页注入不必要的ID导致语法错误

我正在尝试将Spring Data Pageable与JPA Query(非本机)一起使用,这种情况会出现以下异常:

Caused by: java.sql.sqlSyntaxErrorException: Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'alerting.alerthisto0_.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

这是我的查询

@Query(value =
      "SELECT history.alertId AS alertId,history.userId AS userId,alert.organization.id AS organizationId,"
          + " alert.status AS status,alert.createdAt AS createdAt,COUNT(history.alertId) AS alertsCount"
          + " FROM AlertHistory AS history LEFT JOIN Alert AS alert ON history.alertId=alert.id"
          + " GROUP BY history.alertId,history.userId")
  Page<AlertAggregationDetails> groupByAlertIdAndUserId(Pageable pageable);

显然,因为我正在使用Pageable history.id,这是不必要的,并且会引起语法问题。如果我将history.id添加group by部分中,语法将是正确的,但显然,结果与我想要的结果相去甚远。我想知道是否有办法解决语法问题并继续使用Pageable。

此外,如果我删除Pageable,则此查询有效,并且没有问题。

编辑:

调试查询后,我注意到当我使用ORDER BY history.id时,Spring JPA自动查询添加Pageable。作为一种解决方法,我可以替换sql_mode,但是我不明白为什么我不能用Pageable覆盖ORDER BY

解决方法

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。