【问题标题】:Spring Data JPA - Error counting elements of a tableSpring Data JPA - 错误计数表的元素
【发布时间】:2020-05-26 16:52:27
【问题描述】:

我有这个 JPA 查询:

@Query("select count(de) from DeviceEvent de left join de.deviceMessage dm where dm.level = AlarmLevelEnum.HIGH order by de.dateReceived desc ")
    int numDeviceEventsWithAlarm ();

但是当我运行查询时,我收到了这条消息:

2020-05-26 18:46  [http-nio-5678-exec-1] WARN  o.h.e.jdbc.spi.SqlExceptionHelper - SQL Error: 90016, SQLState: 90016
2020-05-26 18:46  [http-nio-5678-exec-1] ERROR o.h.e.jdbc.spi.SqlExceptionHelper - La columna "DEVICEEVEN0_.DATE_RECEIVED" debe estar incluida en la lista de GROUP BY
Column "DEVICEEVEN0_.DATE_RECEIVED" must be in the GROUP BY list; SQL statement:
select count(deviceeven0_.id) as col_0_0_ from t_device_event deviceeven0_ left outer join t_device_message devicemess1_ on deviceeven0_.device_message_id=devicemess1_.id where devicemess1_.level=2 order by deviceeven0_.date_received desc [90016-200]

【问题讨论】:

    标签: mysql hibernate spring-boot jpa spring-data-jpa


    【解决方案1】:

    查询的order by 部分是不必要的,因为您只需要总共count 行。

    因此,您应该以这种方式更正您的查询:

    @Query("select count(de) from DeviceEvent de left join de.deviceMessage dm where dm.level = AlarmLevelEnum.HIGH")
    int numDeviceEventsWithAlarm ();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-16
      • 2019-01-08
      • 1970-01-01
      • 1970-01-01
      • 2018-03-01
      • 1970-01-01
      • 2012-08-15
      相关资源
      最近更新 更多