【问题标题】:How can I return date and count of records for that date using spring data jpa?如何使用 spring data jpa 返回该日期的日期和记录数?
【发布时间】:2019-02-21 07:25:33
【问题描述】:

我想按日期返回事件计数。下面是我的代码

@Query(value = "select DATE(e.startTime) as day,count(distinct e.id) as eventCount from events e " +
            "where e.clientId=:clientId and e.startTime>=:firstDay and e.startTime<=:lastDay group by DATE(e.startTime)",nativeQuery = true)
    Map<Integer, Integer> getClientEventsCountByDay(@Param(value = "clientId") Integer clientId,
                                                    @Param(value = "firstDay") String firstDay,
                                                    @Param(value = "lastDay") String lastDay);

输入是 clientId=2510,firstDay=2019-01-01,lastDay=2019-01-31

此查询抛出错误

原因: org.springframework.dao.IncorrectResultSizeDataAccessException:结果 返回多个元素;嵌套异常是 javax.persistence.NonUniqueResultException:结果返回超过 一个元素

但是,当直接在 db 上运行时,此查询运行良好。

【问题讨论】:

标签: java hibernate spring-data-jpa


【解决方案1】:

试着改成这个

List<Map<Integer, Integer>> getClientEventsCountByDay(@Param(value = "clientId") Integer clientId,
                                                @Param(value = "firstDay") String firstDay,
                                                @Param(value = "lastDay") String lastDay);

【讨论】:

  • 我的目标是返回日期,计为关键值,如相关图片所示。如果我正在获取地图列表,那么每天我都有一张不必要的地图。你能建议实现这一目标的其他方法?最终目标是返回日期并计为键值。
猜你喜欢
  • 2020-04-17
  • 2021-07-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-19
  • 2018-05-22
  • 2020-01-05
  • 2016-05-16
  • 1970-01-01
相关资源
最近更新 更多