【问题标题】:How to get SELECT values and COUNT value with JPA GROUP BY如何使用 JPA GROUP BY 获取 SELECT 值和 COUNT 值
【发布时间】:2017-09-30 08:11:18
【问题描述】:

我看不到如何使用这样的 JPA 查询...

@Query(value = "select statusupdateid, count(*) from cmets group by statusupdateid", nativeQuery = true) 公开的

它应该给出一个 StatusUpdate id 和计数...

我不知道是使用 HashMap 还是什么......但是来自 How to get SELECT values and COUNT value with JPA GROUP BY? 的对象解决方案看起来不像是一个优雅的解决方案,而且我的 HashMap 不起作用

public HashMap<StatusUpdate, Long> topComments();

谢谢

【问题讨论】:

  • 你在使用 Spring Data 吗?
  • 是的。问题必须做更多的事情,您将使用什么来遍历那些(对象,长)数据对。谢谢布山

标签: java mysql hibernate jpa spring-boot


【解决方案1】:

你可以像这样使用它,你需要一个持有 statusupdateid,statusupdateid 的类

使用参数化构造函数:

    class Data {
    //please modify datatype as per your structure
     long statusupdateid;
     long count
    public(long statusupdateid, long count)
    this.statusupdateid=statusupdateid;
    this.count =count;
    }

现在您的查询将:

@Query(value = "select statusupdateid, count(*) from comments group by statusupdateid", nativeQuery = true) 
public List<Data> findStatusupdateidAndCount();

现在它将返回 Data 的列表,其中包含 statusupdateid 并且计数

【讨论】:

    【解决方案2】:
    @Query(value = "select statusupdateid as statusupdateid , count(*) as count from comments group by statusupdateid")
    public List<Map<String,Object>> find();
    

    @Query(value = "select statusupdateid as statusupdateid , count(*) as count from comments group by statusupdateid")
    public Page<Map<String,Object>> find(Pageable pageable);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-15
      • 1970-01-01
      • 1970-01-01
      • 2021-08-06
      相关资源
      最近更新 更多