gpt4 book ai didi

java - Spring data jpa - 返回映射

转载 作者:行者123 更新时间:2023-11-30 07:10:02 25 4
gpt4 key购买 nike

我有一个包含各种值的数据库,所有值都与日期相关。我想要做的是返回一个包含日期以及与该日期相关的所有值的平均值的 map 。每次我提出请求时,我都会收到以下响应:

{
"timestamp": 1473327111738,
"status": 500,
"error": "Internal Server Error",
"exception": "org.springframework.dao.IncorrectResultSizeDataAccessException",
"message": "result returns more than one elements; nested exception is javax.persistence.NonUniqueResultException: result returns more than one elements",
"path": "/dailyAverage"
}

我的代码:

@Query("SELECT du.date, avg(du.usage) FROM DataUsageModel du WHERE du.date BETWEEN :start_time AND :end_time GROUP BY day(du.date)")
Map<String, Double> findDailyAverages(@Param("start_time") LocalDateTime startTime, @Param("end_time") LocalDateTime endTime);

最佳答案

上面是一个 json 对象。必须有代码调用 findDailyAverages 方法并将其转换为 Json 对象。如果您不想更改,则解析 JSONObject(上面的响应)并创建 HashMap

public static void jsonToMap(String t) throws JSONException {

HashMap<String, String> map = new HashMap<String, String>();
JSONObject jObject = new JSONObject(t);
Iterator<?> keys = jObject.keys();

while( keys.hasNext() ){
String key = (String)keys.next();
String value = jObject.getString(key);
map.put(key, value);

}

System.out.println("json : "+jObject);
System.out.println("map : "+map);
}

关于java - Spring data jpa - 返回映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39387582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com