我不明白如何使用这样的 JPA 查询...
@Query(value = "select statusupdateid, count(*) from comments group by statusupdateid", nativeQuery = true) public
它应该给出 StatusUpdate id 和计数...
我不知道是否使用 HashMap 还是什么......但来自 How to get SELECT values and COUNT value with JPA GROUP BY? 的对象解决方案看起来不像是一个优雅的解决方案,而且我的 HashMap 不起作用
public HashMap<StatusUpdate, Long> topComments();
谢谢
最佳答案
你可以像这样使用它,你需要一个带有hold 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();
现在它将返回具有 statusupdateid 和计数的 Data
列表
关于java - 如何使用 JPA GROUP BY 获取 SELECT 值和 COUNT 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43744053/